Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Assets.draco18s.config;
- using Assets.draco18s.dunmake.enums;
- using Assets.draco18s.dunmake.init;
- using Assets.draco18s.dunmake.objects;
- using Assets.draco18s.dunmake.tiles;
- using Assets.draco18s.util;
- using Assets.draco18s.ui;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Serialization;
- using System.Linq;
- using Newtonsoft.Json.Linq;
- namespace Assets.draco18s.dunmake.game {
- public class Main : MonoBehaviour {
- public static Camera cam;
- public static Main instance;
- private GameObject lastBlock;
- private MaterialPropertyBlock block;
- private static List<List<DungeonTile>> world;
- private List<GameObject> flowArrows;
- private Tool activeTool = Tool.DIG;
- private DungeonTile selectedTile;
- private TileObject selectedBuildItem;
- private GameObject selectedGhostInstance;
- private List<GameObject> markers;
- private int availableMana = 0;
- private int dungeonFloorNum = 1;
- private void Awake() {
- cam = Camera.main;
- instance = this;
- TileObjects.init();
- GameObject go;
- world = new List<List<DungeonTile>>();
- flowArrows = new List<GameObject>();
- block = new MaterialPropertyBlock();
- markers = new List<GameObject>();
- for(int x = -55; x <= 55; x++) {
- List<DungeonTile> list = new List<DungeonTile>();
- for(int y = -55; y <= 55; y++) {
- go = Instantiate(PrefabManager.instance.DungeonTile);
- go.transform.position = new Vector3(x, 0, y);
- DungeonTile dt = new DungeonTile(go);
- if(Math.Abs(x) - 50 >= 0 || Math.Abs(y) - 50 >= 0) {
- Destroy(go.GetComponent<Collider>());
- go.GetComponent<Renderer>().GetPropertyBlock(block);
- block.SetColor("_Color", Color.gray);
- go.GetComponent<Renderer>().SetPropertyBlock(block);
- }
- if(Math.Abs(x) - 50 < 0 && Math.Abs(y) - 50 < 0)
- list.Add(dt);
- }
- if(Math.Abs(x) - 50 < 0)
- world.Add(list);
- }
- Localization.initialize();
- InitBlankMap();
- //UIManager.instance.init();
- UIManager.instance.initInventory();
- /*JsonSerializerSettings settings = new JsonSerializerSettings();
- settings.ContractResolver = new Main.ContractResolver();
- //settings.TypeNameHandling = TypeNameHandling.ALL;
- string json = JsonConvert.SerializeObject(world[40][40], Formatting.Indented, settings);
- //Debug.Log(json);
- //world = JsonConvert.DeserializeObject<List<List<DungeonTile>>>(json, settings);*/
- }
- private static void InitBlankMap() {
- SetEmpty(world[40][40].dispObj);
- SetEmpty(world[40][41].dispObj);
- SetEmpty(world[40][42].dispObj);
- SetEmpty(world[40][43].dispObj);
- SetEmpty(world[40][44].dispObj);
- SetEmpty(world[40][45].dispObj);
- SetEmpty(world[39][42].dispObj);
- SetEmpty(world[38][42].dispObj);
- instance.activeTool = Tool.PLACE;
- instance.selectedBuildItem = TileObjects.EntranceStairs;
- instance.lastBlock = instance.GetTileAt(40, 40).dispObj;
- instance.UseActiveTool();
- instance.activeTool = Tool.PLACE;
- instance.selectedBuildItem = TileObjects.DungeonCore;
- instance.lastBlock = instance.GetTileAt(40, 45).dispObj;
- instance.UseActiveTool();
- instance.lastBlock = instance.GetTileAt(38, 42).dispObj;
- instance.activeTool = Tool.PLACE;
- instance.selectedBuildItem = TileObjects.TreasureChest;
- instance.UseActiveTool();
- instance.GetTileAt(38, 42).tileObjRef.RightClick(instance.GetTileAt(38, 42), instance.GetTileAt(38, 42).tileObjGo);
- instance.activeTool = Tool.DIG;
- instance.RecalculateFlow();
- instance.CalculateCosts();
- }
- public static void settingsOnValueChanged(Dropdown dropdown) {
- instance.selectedTile.tileObjRef.OnSettingChanged(instance.selectedTile, dropdown.options[dropdown.value]);
- UIManager.DisableListeners();
- instance.selectedTile.tileObjRef.OnSelected(instance.selectedTile, dropdown);
- UIManager.EnsableListeners();
- instance.selectedTile.tileObjRef.OnAdded(instance.selectedTile, instance.selectedTile.tileObjGo);
- instance.RecalculateFlow();
- }
- private void Update() {
- SelectAndClickBlocks();
- MoveCamera();
- Button btn = UIManager.instance.lowerui.transform.Find("DigButton").GetComponent<Button>();
- ColorBlock cc = btn.colors;
- cc.normalColor = activeTool == Tool.DIG?ColorHelper.LBLUE : Color.white;
- cc.highlightedColor = activeTool == Tool.DIG ? ColorHelper.LBLUE : Color.white;
- btn.colors = cc;
- btn = UIManager.instance.lowerui.transform.Find("ClearButton").GetComponent<Button>();
- cc = btn.colors;
- cc.normalColor = activeTool == Tool.CLEAR ? ColorHelper.LBLUE : Color.white;
- cc.highlightedColor = activeTool == Tool.CLEAR ? ColorHelper.LBLUE : Color.white;
- btn.colors = cc;
- btn = UIManager.instance.lowerui.transform.Find("WireButton").GetComponent<Button>();
- cc = btn.colors;
- cc.normalColor = activeTool == Tool.WIRES ? ColorHelper.LBLUE : Color.white;
- cc.highlightedColor = activeTool == Tool.WIRES ? ColorHelper.LBLUE : Color.white;
- btn.colors = cc;
- btn = UIManager.instance.lowerui.transform.Find("SelectButton").GetComponent<Button>();
- cc = btn.colors;
- cc.normalColor = activeTool == Tool.SELECT ? ColorHelper.LBLUE : Color.white;
- cc.highlightedColor = activeTool == Tool.SELECT ? ColorHelper.LBLUE : Color.white;
- btn.colors = cc;
- }
- private void SelectAndClickBlocks() {
- RaycastHit hit;
- Ray ray = cam.ScreenPointToRay(Input.mousePosition);
- if(Physics.Raycast(ray, out hit)) {
- if(lastBlock != null && lastBlock != hit.transform.gameObject) {
- lastBlock.GetComponent<Renderer>().GetPropertyBlock(block);
- block.SetColor("_Color", Color.white);
- lastBlock.GetComponent<Renderer>().SetPropertyBlock(block);
- }
- lastBlock = hit.transform.gameObject;
- if(activeTool == Tool.DIG) {
- lastBlock.GetComponent<Renderer>().GetPropertyBlock(block);
- block.SetColor("_Color", Color.red);
- lastBlock.GetComponent<Renderer>().SetPropertyBlock(block);
- }
- if(EventSystem.current != null && !EventSystem.current.IsPointerOverGameObject()) {
- if(Input.GetMouseButtonUp(0)) {
- UseActiveTool();
- }
- if(Input.GetMouseButtonUp(1)) {
- DungeonTile dt = GetTileAt(lastBlock.transform.position);
- if((dt.state & TileState.FILLED) == 0) {
- if(dt.tileObjRef != null) {
- dt.tileObjRef.RightClick(dt, dt.tileObjGo);
- }
- }
- }
- if(Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)) {
- RecalculateFlow();
- CalculateCosts();
- }
- else if(selectedGhostInstance) {
- DungeonTile dt = GetTileAt(lastBlock.transform.position);
- if((dt.state & TileState.FILLED) == 0) {
- selectedGhostInstance.SetActive(true);
- selectedGhostInstance.transform.position = lastBlock.transform.position + Vector3.up;
- }
- else {
- selectedGhostInstance.SetActive(false);
- }
- }
- }
- }
- else if(lastBlock != null) {
- lastBlock.GetComponent<Renderer>().GetPropertyBlock(block);
- block.SetColor("_Color", Color.white);
- lastBlock.GetComponent<Renderer>().SetPropertyBlock(block);
- lastBlock = null;
- if(selectedGhostInstance)
- selectedGhostInstance.SetActive(false);
- }
- }
- private void UseActiveTool() {
- if(activeTool == Tool.SELECT) {
- DungeonTile dt = GetTileAt(lastBlock.transform.position);
- if(selectedTile != dt && selectedTile != null)
- selectedTile.tileObjRef.OnUnselected(selectedTile);
- if(dt.tileObjRef != null && dt.tileObjRef.HasSelectionWindow(dt)) {
- Vector3 vec = cam.WorldToScreenPoint(dt.dispObj.transform.position);
- vec.z = 0;
- vec.x -= 100;
- vec.y += 30;
- if(vec.x < 90) {
- vec.x += 190;
- }
- vec.y = Math.Max(vec.y, 235);
- vec.y = Math.Min(vec.y, 635);
- vec.x -= Screen.width / 2;
- vec.y -= Screen.height / 2;
- UIManager.instance.tileObjSettingsPanel.transform.localPosition = vec;
- UIManager.instance.tileObjSettingsPanel.SetActive(true);
- UIManager.DisableListeners();
- Dropdown[] l = UIManager.instance.tileObjSettingsPanel.GetComponentsInChildren<Dropdown>(true);
- foreach(Dropdown dd in l) {
- dt.tileObjRef.OnSelected(dt, dd);
- dd.gameObject.SetActive(dd.options.Count > 0);
- }
- UIManager.EnsableListeners();
- selectedTile = dt;
- }
- else {
- UIManager.instance.tileObjSettingsPanel.SetActive(false);
- }
- }
- if(activeTool == Tool.DIG) {
- if(Math.Abs(lastBlock.transform.position.x) - 50 < 0 && Math.Abs(lastBlock.transform.position.z) - 50 < 0) {
- if(lastBlock.transform.position.y >= 0) {
- SetEmpty(lastBlock);
- }
- else {
- SetFilled(lastBlock);
- }
- }
- }
- if(activeTool == Tool.PLACE) {
- DungeonTile dt = GetTileAt(lastBlock.transform.position);
- ClearTile(dt);
- if((dt.state & TileState.FILLED) == 0) {
- GameObject go = Instantiate(selectedBuildItem.prefab);
- go.transform.position = lastBlock.transform.position + Vector3.up;
- dt.tileObjGo = go;
- dt.tileObjRef = selectedBuildItem;
- dt.tileObjFacing = dt.tileObjRef.GetDefaultFacing();
- dt.tileObjRef.OnAdded(dt, dt.tileObjGo);
- }
- if(!Input.GetButton("Shift"))
- SetActiveTool(Tool.DIG);
- }
- if(activeTool == Tool.CLEAR) {
- DungeonTile dt = GetTileAt(lastBlock.transform.position);
- if((dt.state & TileState.FILLED) == 0) {
- ClearTile(dt);
- }
- }
- if(activeTool == Tool.WIRES) {
- DungeonTile dt = GetTileAt(lastBlock.transform.position);
- if((dt.state & TileState.FILLED) == 0) {
- ManaChannelState st = dt.wireState;
- if(st == ManaChannelState.NONE)
- st = ManaChannelState.INACTIVE;
- else
- st = ManaChannelState.NONE;
- dt.SetWireState(st);
- }
- }
- }
- public static void SetActiveTool(Tool tool) {
- UIManager.instance.tileObjSettingsPanel.SetActive(false);
- instance.activeTool = tool;
- Destroy(instance.selectedGhostInstance);
- instance.selectedGhostInstance = null;
- if(instance.selectedTile != null) {
- instance.selectedTile.tileObjRef.OnUnselected(instance.selectedTile);
- instance.selectedTile = null;
- }
- }
- public static void SetActiveTool(Tool tool, object obj) {
- UIManager.instance.tileObjSettingsPanel.SetActive(false);
- instance.activeTool = tool;
- if(obj is TileObject) {
- Destroy(instance.selectedGhostInstance);
- instance.selectedGhostInstance = null;
- instance.selectedBuildItem = (TileObject)obj;
- instance.selectedGhostInstance = Instantiate(instance.selectedBuildItem.prefab);
- instance.selectedGhostInstance.SetActive(false);
- Renderer[] renderers = instance.selectedGhostInstance.GetComponentsInChildren<Renderer>();
- bool areAnyMasked = false;
- foreach(Renderer r in renderers) {
- areAnyMasked = r.material.shader.ToString().StartsWith("Mask");
- }
- if(!areAnyMasked) {
- Shader sh = Shader.Find("Custom/Ghost");
- foreach(Renderer r in renderers) {
- r.material.shader = sh;
- }
- }
- else {
- //Shader sh = Shader.Find("Custom/Ghost2");
- //foreach(Renderer r in renderers) {
- //r.material.shader = sh;
- //}
- }
- }
- if(instance.selectedTile != null) {
- instance.selectedTile.tileObjRef.OnUnselected(instance.selectedTile);
- instance.selectedTile = null;
- }
- }
- private static void ShowFlowError(FlowError err) {
- UIManager.ShowError(err.ToString());
- }
- private int CalculateCosts() {
- int val = 0;
- for(int x = 0; x < 99; x++) {
- for(int y = 0; y < 99; y++) {
- if((world[x][y].state & TileState.FILLED) == 0)
- if(world[x][y].tileObjRef != null)
- val += world[x][y].tileObjRef.buildCost;
- }
- }
- int max = (20 + dungeonFloorNum * 5);
- max *= max;
- max *= 2;
- UIManager.instance.availableMana.text = Localization.translateToLocal("ui.available_mana.text",val,availableMana,max);
- if(val > availableMana) {
- UIManager.ShowError("ui.overspent.text");
- }
- if(availableMana > max) {
- UIManager.ShowError("ui.floor_too_large.text");
- }
- return val;
- }
- private void RecalculateFlow() {
- foreach(GameObject go in markers) {
- Destroy(go);
- }
- foreach(GameObject go in flowArrows) {
- Destroy(go);
- }
- FlowError err = DoRecalculateFlow();
- if(err != FlowError.NONE) {
- ShowFlowError(err);
- }
- else {
- foreach(GameObject go in markers) {
- Destroy(go);
- }
- foreach(GameObject go in flowArrows) {
- Destroy(go);
- }
- UIManager.ShowError("");
- }
- }
- private FlowError DoRecalculateFlow() {
- FlowError result;
- List<DungeonTile> allof;
- List<DungeonTile> unescapeSet = new List<DungeonTile>();
- List<DungeonTile> recurseSet = new List<DungeonTile>();
- for(int x = 0; x < 99; x++) {
- for(int y = 0; y < 99; y++) {
- if(world[x][y].tileObjRef is IFlowAlteringSource)
- ((IFlowAlteringSource)world[x][y].tileObjRef).UpdateFlow(world[x][y], new Vector2Int(x, y), FlowRestriction.UNKNOWN);
- }
- }
- allof = FindAllTilesOfType(TileState.SURFACE_ACCESS_ENTR);
- if(allof.Count > 1)
- return FlowError.MORE_THAN_ONE_ENTRANCE;
- if(allof.Count == 0)
- return FlowError.MISSING_ENTRANCE;
- allof = FindAllTilesOfType(TileState.CORE);
- if(allof.Count > 1)
- return FlowError.MORE_THAN_ONE_CORE;
- if(allof.Count == 0)
- return FlowError.MISSING_CORE;
- foreach(DungeonTile dt in allof) {
- result = RecalculateReachabilityFrom(dt, SecretDoor.DENY, ref unescapeSet, WireRecurse.YES, ref recurseSet);
- if(result != FlowError.NONE && !(result == FlowError.UNESCAPABLE_AREA1 || result == FlowError.UNESCAPABLE_AREA2))
- return result;
- }
- for(int x = 0; x < 99; x++) {
- for(int y = 0; y < 99; y++) {
- if((world[x][y].state & TileState.FILLED) == 0)
- unescapeSet.Add(world[x][y]);
- }
- }
- availableMana = unescapeSet.Count * 2;
- allof = FindAllTilesOfType(TileState.SURFACE_ACCESS_EXIT);
- //allof.RemoveAt(allof.FindIndex(x => (x.state & TileState.SURFACE_ACCESS_ENTR) > 0));
- result = FlowError.NONE;
- foreach(DungeonTile dt in allof) {
- result |= RecalculateReachabilityFrom(dt, SecretDoor.DENY, ref unescapeSet, WireRecurse.NONE, ref recurseSet);
- }
- unescapeSet.RemoveAll(x => x.tileObjRef != null && x.tileObjRef is IUnwalkable);
- if(((result & FlowError.UNESCAPABLE_AREA1) > 0 || (result & FlowError.UNESCAPABLE_AREA2) > 0) && unescapeSet.Count > 0) {
- int max = 30;
- DungeonTile c = FindAllTilesOfType(TileState.CORE)[0];
- unescapeSet.Sort((x, y) => (x.position - c.position).sqrMagnitude.CompareTo((y.position - c.position).sqrMagnitude));
- foreach(DungeonTile udt in unescapeSet) {
- GameObject arr = Instantiate(PrefabManager.instance.MarkerArrow);
- markers.Add(arr);
- arr.transform.position = udt.dispObj.transform.position + Vector3.up * 1.5f;
- max--;
- if(max <= 0) break;
- }
- return FlowError.UNESCAPABLE_AREA1;
- }
- result &= ~(FlowError.UNESCAPABLE_AREA1);
- result &= ~(FlowError.UNESCAPABLE_AREA2);
- if(result != FlowError.NONE) {
- return result;
- }
- /*allof = FindAllTilesOfType(TileState.CORE);
- foreach(DungeonTile dt in allof) {
- result = RecalculateReachabilityFrom(dt, ref unescapeSet);
- }*/
- foreach(GameObject go in flowArrows) {
- Destroy(go);
- }
- List<DungeonTile> obstructed = new List<DungeonTile>();
- result = ValidateObjectPlacement(ref obstructed);
- foreach(DungeonTile odt in obstructed) {
- GameObject arr = Instantiate(PrefabManager.instance.MarkerArrow);
- markers.Add(arr);
- arr.transform.position = odt.dispObj.transform.position + Vector3.up * 1.5f;
- }
- if(result == FlowError.NONE) {
- allof = FindAllTilesOfType(TileState.REWARD);
- if(allof.Count == 0)
- return FlowError.NO_REWARDS;
- unescapeSet.Clear();
- FlowError result2 = FlowError.NONE;
- foreach(DungeonTile dt in allof) {
- result2 = RecalculateReachabilityFrom(dt, SecretDoor.ALLOW, ref unescapeSet, WireRecurse.YES, ref recurseSet);
- if(result2 != FlowError.NONE && result2 != FlowError.UNESCAPABLE_AREA2)
- return FlowError.UNREACHABLE_AREA;
- }
- }
- return result;
- }
- private List<DungeonTile> FindAllTilesOfType(TileState type) {
- List<DungeonTile> searchSet = new List<DungeonTile>();
- for(int x = 0; x < 99; x++) {
- for(int y = 0; y < 99; y++) {
- if((world[x][y].state & type) > 0)
- searchSet.Add(world[x][y]);
- }
- }
- return searchSet;
- }
- private FlowError RecalculateReachabilityFrom(DungeonTile center, SecretDoor secrets, ref List<DungeonTile> unescapables, WireRecurse followWire, ref List<DungeonTile> recurseSet) {
- foreach(GameObject go in flowArrows) {
- Destroy(go);
- }
- List<DungeonTile> dugSet = new List<DungeonTile>();
- for(int x = 0; x < 99; x++) {
- for(int y = 0; y < 99; y++) {
- if((world[x][y].state & TileState.FILLED) == 0)
- dugSet.Add(world[x][y]);
- }
- }
- if(center == null && (center.state & TileState.CORE) > 0) return FlowError.MISSING_CORE;
- unescapables.Remove(center);
- DungeonTilePath coreTilePath = new DungeonTilePath(center, null);
- List<DungeonTilePath> openSet = new List<DungeonTilePath>();
- List<DungeonTilePath> closedSet = new List<DungeonTilePath>();
- for(int x = -1; x <= 1; x++) {
- for(int y = -1; y <= 1; y++) {
- if(x == 0 && y == 0) continue;
- DungeonTile dt = GetTileAt(x + center.position.x, y + center.position.y);
- if((dt.state & TileState.FILLED) == 0) {
- DungeonTilePath ths = new DungeonTilePath(dt, coreTilePath);
- if((FlowAllowed(ths) || (secrets == SecretDoor.ALLOW && (ths.tile.tileObjRef == TileObjects.SecretDoor || ths.next.tile.tileObjRef == TileObjects.SecretDoor))) && checkDiagonal(ths))
- openSet.Add(ths);
- }
- }
- }
- while(openSet.Count > 0) {
- DungeonTilePath dp = openSet[0];
- openSet.RemoveAt(0);
- if(dp.tile.wireState == ManaChannelState.INACTIVE && (dp.tile.tileObjRef is Door) && ((Door)dp.tile.tileObjRef).isOneWay == false) {
- if(followWire == WireRecurse.YES && !recurseSet.Contains(dp.tile)) {
- List<DungeonTile> unescapeSet = FindWireActivation(dp.tile);
- if(unescapeSet.Count == 0) {
- GameObject arr = Instantiate(PrefabManager.instance.MarkerArrow);
- markers.Add(arr);
- arr.transform.position = dp.tile.dispObj.transform.position + Vector3.up * 1.5f;
- return FlowError.UNACTIVATABLE_OBJECT;
- }
- List<DungeonTile> allof = FindAllTilesOfType(TileState.SURFACE_ACCESS_ENTR);
- FlowError result = FlowError.NONE;
- recurseSet.Add(dp.tile);
- foreach(DungeonTile dt in allof) {
- result |= RecalculateReachabilityFrom(dt, secrets, ref unescapeSet, WireRecurse.YES, ref recurseSet);
- }
- if(unescapeSet.Count > 0) {
- foreach(DungeonTile dt in unescapeSet) {
- GameObject arr = Instantiate(PrefabManager.instance.MarkerArrow);
- markers.Add(arr);
- arr.transform.position = dt.dispObj.transform.position + Vector3.up * 1.5f;
- }
- return FlowError.UNREACHABLE_OBJECTIVE;
- }
- }
- else if(followWire == WireRecurse.INPROGRESS) {
- continue;
- }
- }
- unescapables.Remove(dp.tile);
- dugSet.Remove(dp.tile);
- closedSet.Add(dp);
- GameObject arrow = Instantiate(PrefabManager.instance.FlowArrow);
- arrow.transform.position = dp.tile.dispObj.transform.position;
- flowArrows.Add(arrow);
- arrow.transform.LookAt(dp.next.tile.dispObj.transform);
- for(int x = -1; x <= 1; x++) {
- for(int y = -1; y <= 1; y++) {
- if(x == 0 && y == 0) continue;
- DungeonTile dt = GetTileAt(x + dp.tile.position.x, y + dp.tile.position.y);
- DungeonTilePath ths = new DungeonTilePath(dt, dp);
- if((dt.state & TileState.FILLED) == 0 && openSet.FindIndex(p => p.tile == dt) < 0 && closedSet.FindIndex(p => p.tile == dt) < 0) {
- if(secrets == SecretDoor.ALLOW && (ths.tile.tileObjRef == TileObjects.SecretDoor || ths.next.tile.tileObjRef == TileObjects.SecretDoor)) {
- Debug.Log("Door allowed");
- }
- if(((FlowAllowed(ths) && checkDiagonal(ths)) || (secrets == SecretDoor.ALLOW && (ths.tile.tileObjRef == TileObjects.SecretDoor || ths.next.tile.tileObjRef == TileObjects.SecretDoor)))) {
- openSet.Add(ths);
- }
- }
- }
- }
- openSet.Sort((x, y) => x.distance.CompareTo(y.distance));
- }
- FlowError res = closedSet.FindIndex(x => (x.tile.state & TileState.SURFACE_ACCESS_ENTR) > 0) >= 0 ? FlowError.NONE : FlowError.UNREACHABLE_CORE;
- if(res != FlowError.NONE) {
- if((center.state & TileState.CORE) > 0)
- return res;
- return FlowError.UNESCAPABLE_AREA1;
- }
- if(dugSet.Count > 0) return FlowError.UNESCAPABLE_AREA2;
- return res;
- }
- private List<DungeonTile> FindWireActivation(DungeonTile coreTile) {
- DungeonTilePath coreTilePath = new DungeonTilePath(coreTile, null);
- List<DungeonTilePath> openSet = new List<DungeonTilePath>();
- List<DungeonTilePath> closedSet = new List<DungeonTilePath>();
- for(int x = -1; x <= 1; x++) {
- for(int y = -1; y <= 1; y++) {
- if(Math.Abs(x) + Math.Abs(y) != 1) continue;
- DungeonTile dt = GetTileAt(x + coreTile.position.x, y + coreTile.position.y);
- if((dt.wireState != ManaChannelState.NONE)) {
- DungeonTilePath ths = new DungeonTilePath(dt, coreTilePath);
- openSet.Add(ths);
- }
- }
- }
- while(openSet.Count > 0) {
- DungeonTilePath dp = openSet[0];
- openSet.RemoveAt(0);
- closedSet.Add(dp);
- for(int x = -1; x <= 1; x++) {
- for(int y = -1; y <= 1; y++) {
- if(Math.Abs(x) + Math.Abs(y) != 1) continue;
- DungeonTile dt = GetTileAt(x + dp.tile.position.x, y + dp.tile.position.y);
- if((dt.wireState != ManaChannelState.NONE) && openSet.FindIndex(p => p.tile == dt) < 0 && closedSet.FindIndex(p => p.tile == dt) < 0) {
- DungeonTilePath ths = new DungeonTilePath(dt, dp);
- openSet.Add(ths);
- }
- }
- }
- openSet.Sort((x, y) => x.distance.CompareTo(y.distance));
- }
- List<DungeonTilePath> list = closedSet.FindAll(x => x.tile.wireState == ManaChannelState.ACTIVE);
- List<DungeonTile> ret = new List<DungeonTile>();
- foreach(DungeonTilePath dtp in list) {
- ret.Add(dtp.tile);
- }
- return ret;
- }
- private FlowError ValidateObjectPlacement(ref List<DungeonTile> errorLoc) {
- for(int x = 0; x < 99; x++) {
- for(int y = 0; y < 99; y++) {
- if(world[x][y].tileObjRef != null && world[x][y].tileObjRef.buffer > 0) {
- if(!ValidateBuffer(world[x][y], world[x][y].tileObjRef.buffer, ref errorLoc)) {
- errorLoc.Add(world[x][y]);
- }
- }
- }
- }
- if(errorLoc.Count > 0) return FlowError.CROWDED_OBJECT;
- return FlowError.NONE;
- }
- private bool ValidateBuffer(DungeonTile center, int dist, ref List<DungeonTile> errorLoc) {
- bool ret = true;
- if(dist <= 0) return ret;
- for(int x = -dist; x <= dist; x++) {
- for(int y = -dist; y <= dist; y++) {
- if(x == 0 && y == 0) continue;
- DungeonTile dt = GetTileAt(x + center.position.x, y + center.position.y);
- if(dt.tileObjRef != null && !(dt.tileObjRef is IUnwalkable)) {
- errorLoc.Add(dt);
- ret = false;
- }
- }
- }
- return ret;
- }
- private bool FlowAllowed(DungeonTilePath dp) {
- if(dp.tile == dp.next.tile) {
- return true;
- }
- FlowRestriction flowDirOut = FlowRestriction.UNKNOWN;
- FlowRestriction flowDirIn = FlowRestriction.UNKNOWN;
- if(dp.next.tile.position.x - dp.tile.position.x > 0) {
- flowDirOut |= FlowRestriction.EAST;
- flowDirIn |= FlowRestriction.WEST;
- }
- if(dp.next.tile.position.x - dp.tile.position.x < 0) {
- flowDirOut |= FlowRestriction.WEST;
- flowDirIn |= FlowRestriction.EAST;
- }
- if(dp.next.tile.position.y - dp.tile.position.y > 0) {
- flowDirOut |= FlowRestriction.NORTH;
- flowDirIn |= FlowRestriction.SOUTH;
- }
- if(dp.next.tile.position.y - dp.tile.position.y < 0) {
- flowDirOut |= FlowRestriction.SOUTH;
- flowDirIn |= FlowRestriction.NORTH;
- }
- if(flowDirOut == FlowRestriction.UNKNOWN) {
- Debug.Log("FLARGLEBLARG");
- }
- return (flowDirOut & dp.tile.allowedDirectionsOut) == flowDirOut && (flowDirIn & dp.next.tile.allowedDirectionsIn) > 0;
- }
- private bool checkDiagonal(DungeonTilePath dp) {
- DungeonTile dt1 = GetTileAt(dp.next.tile.position.x, dp.tile.position.y);
- DungeonTile dt2 = GetTileAt(dp.tile.position.x, dp.next.tile.position.y);
- bool p1 = !((dt1.state & TileState.FILLED) > 0 || (dt2.state & TileState.FILLED) > 0);
- DungeonTilePath dpa = new DungeonTilePath(dt1, dp.next);
- DungeonTilePath dpb = new DungeonTilePath(dt2, dp.next);
- bool p2 = FlowAllowed(dpa) && FlowAllowed(dpb);
- bool p3 = FlowAllowed(new DungeonTilePath(dp.tile, dpa)) && FlowAllowed(new DungeonTilePath(dp.tile, dpb));
- //Debug.Log(p2);
- return p1 && p2 && p3;
- }
- public DungeonTile GetTileAt(Vector3 pos) {
- int x = (int)pos.x + 49;
- int y = (int)pos.z + 49;
- return GetTileAt(x, y);
- }
- public DungeonTile GetTileAt(int x, int y) {
- if(x >= world.Count || x < 0 || y >= world.Count || y < 0) return null;
- return world[x][y];
- }
- private static void SetFilled(GameObject lastBlock) {
- DungeonTile dt = world[(int)lastBlock.transform.position.x + 49][(int)lastBlock.transform.position.z + 49];
- if((dt.state & TileState.CORE) > 0 || (dt.state & TileState.SURFACE_ACCESS_ENTR) > 0) {
- return;
- }
- lastBlock.transform.position = new Vector3(lastBlock.transform.position.x, 0, lastBlock.transform.position.z);
- //TODO: clear decorations and objects
- ClearTile(dt);
- dt.allowedDirectionsOut = (FlowRestriction.NORTH | FlowRestriction.SOUTH | FlowRestriction.EAST | FlowRestriction.WEST);
- dt.state |= TileState.FILLED;
- dt.SetWireState(ManaChannelState.NONE);
- dt.tileObjMetadata = 0;
- }
- private static void ClearTile(DungeonTile dt) {
- if(dt.tileObjRef != null) {
- dt.tileObjRef.OnRemoved(dt, dt.tileObjGo);
- dt.tileObjRef = null;
- Destroy(dt.tileObjGo);
- dt.tileObjGo = null;
- dt.state &= TileState.FILLED;
- dt.tileObjFacing = Facing.UNKNOWN;
- dt.allowedDirectionsIn = dt.allowedDirectionsOut = (FlowRestriction.NORTH | FlowRestriction.SOUTH | FlowRestriction.EAST | FlowRestriction.WEST);
- }
- //dt.decorationRef
- }
- private static void SetEmpty(GameObject lastBlock) {
- DungeonTile dt = world[(int)lastBlock.transform.position.x + 49][(int)lastBlock.transform.position.z + 49];
- lastBlock.transform.position = new Vector3(lastBlock.transform.position.x, -1, lastBlock.transform.position.z);
- dt.state &= ~TileState.FILLED;
- }
- private void MoveCamera() {
- if(EventSystem.current != null && EventSystem.current.currentSelectedGameObject == UIManager.instance.searchInput) {
- return;
- }
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- Vector3 r = cam.transform.right;
- Vector3 f = cam.transform.forward;
- Vector3 move = new Vector3(h, 0, v);
- move = cam.transform.TransformVector(move);
- move.y = 0;
- move = cam.transform.localPosition + move.normalized * Time.deltaTime * 7;
- move = MathHelper.clamp(move, new Rect(-38, -38, 93, 93));
- move = move - cam.transform.position;
- move = cam.transform.localPosition + move.normalized * Time.deltaTime * 7;
- move = MathHelper.clamp(move, new Rect(-38, -38, 93, 93));
- cam.transform.localPosition = move;
- //-38,-38
- //55,55
- }
- private enum WireRecurse {
- NONE,
- INPROGRESS,
- YES
- }
- public class ContractResolver : DefaultContractResolver {
- public override JsonContract ResolveContract(Type type) {
- JsonContract contract = base.ResolveContract(type);
- if(typeof(TileObject).IsAssignableFrom(type)) {
- contract.Converter = new TileObject.Converter();
- }
- if(typeof(Vector2Int).IsAssignableFrom(type)) {
- contract.Converter = new Vector2IntConverter();
- }
- return contract;
- }
- protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) {
- IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
- foreach(JsonProperty prop in properties) {
- prop.DefaultValueHandling = DefaultValueHandling.Ignore;
- }
- return properties;
- }
- }
- public class Vector2IntConverter : JsonConverter {
- public override bool CanConvert(Type objectType) {
- return typeof(Vector2Int).IsAssignableFrom(objectType);
- }
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
- JObject jObject = JObject.Load(reader);
- return new Vector2Int((int)jObject.GetValue("x"), (int)jObject.GetValue("y"));
- }
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
- Vector2Int v = (Vector2Int)value;
- JObject o = new JObject();
- o.Add(new JProperty("x", v.x));
- o.Add(new JProperty("y", v.y));
- o.WriteTo(writer);
- }
- public override bool CanRead {
- get { return true; }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement