2020-11-27 22:47:33 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2020-11-29 22:50:12 +00:00
|
|
|
|
using Cinemachine;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
using MessagePack;
|
2020-11-29 22:50:12 +00:00
|
|
|
|
using UniRx;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
|
using static Unity.Mathematics.math;
|
|
|
|
|
|
using float2 = Unity.Mathematics.float2;
|
|
|
|
|
|
|
|
|
|
|
|
public class ActionGameManager : MonoBehaviour
|
|
|
|
|
|
{
|
2020-12-01 00:16:11 +00:00
|
|
|
|
public GameSettings Settings;
|
2020-11-29 22:50:12 +00:00
|
|
|
|
public SectorRenderer SectorRenderer;
|
|
|
|
|
|
public CinemachineVirtualCamera TopDownCamera;
|
|
|
|
|
|
public CinemachineVirtualCamera FollowCamera;
|
|
|
|
|
|
|
|
|
|
|
|
private DirectoryInfo _filePath;
|
|
|
|
|
|
private bool _editMode;
|
|
|
|
|
|
private float _time;
|
|
|
|
|
|
|
2020-11-27 22:47:33 +00:00
|
|
|
|
public DatabaseCache Cache { get; private set; }
|
|
|
|
|
|
public GameContext Context { get; private set; }
|
2020-11-29 22:50:12 +00:00
|
|
|
|
public Zone Zone { get; private set; }
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
|
{
|
2020-11-29 22:50:12 +00:00
|
|
|
|
ConsoleController.MessageReceiver = this;
|
|
|
|
|
|
_filePath = new DirectoryInfo(Application.dataPath).Parent.CreateSubdirectory("GameData");
|
2020-11-27 22:47:33 +00:00
|
|
|
|
Cache = new DatabaseCache();
|
2020-11-29 22:50:12 +00:00
|
|
|
|
Cache.Load(_filePath.FullName);
|
2020-11-27 22:47:33 +00:00
|
|
|
|
Context = new GameContext(Cache, Debug.Log);
|
|
|
|
|
|
|
2020-11-29 22:50:12 +00:00
|
|
|
|
var zoneFile = Path.Combine(_filePath.FullName, "Home.zone");
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
|
|
// If the game has already been run, there will be a Home file containing a ZonePack; if not, generate one
|
|
|
|
|
|
var zonePack = File.Exists(zoneFile) ?
|
|
|
|
|
|
MessagePackSerializer.Deserialize<ZonePack>(File.ReadAllBytes(zoneFile)) :
|
|
|
|
|
|
ZoneGenerator.GenerateZone(
|
2020-12-01 00:16:11 +00:00
|
|
|
|
settings: Settings.ZoneSettings,
|
|
|
|
|
|
mapLayers: Context.MapLayers,
|
2020-11-27 22:47:33 +00:00
|
|
|
|
resources: Context.Resources,
|
2020-12-01 00:16:11 +00:00
|
|
|
|
mass: Settings.DefaultZoneMass,
|
|
|
|
|
|
radius: Settings.DefaultZoneRadius
|
2020-11-27 22:47:33 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
|
Zone = new Zone(Settings.PlanetSettings, zonePack);
|
2020-11-29 22:50:12 +00:00
|
|
|
|
SectorRenderer.LoadZone(Zone);
|
|
|
|
|
|
|
|
|
|
|
|
ConsoleController.AddCommand("editmode", _ => ToggleEditMode());
|
2020-12-01 00:16:11 +00:00
|
|
|
|
ConsoleController.AddCommand("savezone", args =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.Length > 0)
|
|
|
|
|
|
Zone.Data.Name = args[0];
|
|
|
|
|
|
SaveZone();
|
|
|
|
|
|
});
|
2020-11-27 22:47:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-01 00:16:11 +00:00
|
|
|
|
public void SaveZone() => File.WriteAllBytes(
|
|
|
|
|
|
Path.Combine(_filePath.FullName, $"{Zone.Data.Name}.zone"), MessagePackSerializer.Serialize(Zone.Pack()));
|
|
|
|
|
|
|
2020-11-29 22:50:12 +00:00
|
|
|
|
public void ToggleEditMode()
|
2020-11-27 22:47:33 +00:00
|
|
|
|
{
|
2020-11-29 22:50:12 +00:00
|
|
|
|
_editMode = !_editMode;
|
|
|
|
|
|
FollowCamera.gameObject.SetActive(!_editMode);
|
|
|
|
|
|
TopDownCamera.gameObject.SetActive(_editMode);
|
2020-11-27 22:47:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-29 22:50:12 +00:00
|
|
|
|
void Update()
|
2020-11-27 22:47:33 +00:00
|
|
|
|
{
|
2020-11-29 22:50:12 +00:00
|
|
|
|
if(!_editMode)
|
|
|
|
|
|
{
|
|
|
|
|
|
_time += Time.deltaTime;
|
|
|
|
|
|
Context.Time = Time.time;
|
|
|
|
|
|
}
|
2020-12-01 00:16:11 +00:00
|
|
|
|
Zone.Update(_time, Time.deltaTime);
|
2020-12-01 20:38:28 +00:00
|
|
|
|
SectorRenderer.Time = _time;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|