public Vector2Int NodePositionFromWorld(o_node node) { int x = Mathf.RoundToInt(node.position.x / nodesize); int y = Mathf.RoundToInt(node.position.y / nodesize); return(new Vector2Int(x, y)); }
public int MoveCost(Vector3 cursorpos) { Vector3 mousePositon = cursorpos; if (path.PathFind(transform.position, mousePositon)) { o_node goal = grid.NodeFromWorld(mousePositon); o_node thischaracter = grid.NodeFromWorld(transform.position); if (!grid.NodeFromWorld(mousePositon).walkable) { s_object objectm = grid.ObjectFromWorld(mousePositon); return(-1); } else { return(path.RetracePath(goal, thischaracter).Count); } } else { return(-1); } }
public s_object ObjectFromWorld(o_node node) { int x = Mathf.RoundToInt(node.position.x / nodesize); int y = Mathf.RoundToInt(node.position.y / nodesize); if (item_layer[x, y] == null && character_layer[x, y] == null && block_layer[x, y] == null || (x > gridworldsize.x - 1 || x < 0 || y <0 || y> gridworldsize.y - 1)) { return(null); } if (character_layer[x, y] != null) { return(character_layer[x, y]); } if (item_layer[x, y] != null) { return(item_layer[x, y]); } if (block_layer[x, y] != null) { return(block_layer[x, y]); } return(null); }
public List <o_node> CheckAroundNode(o_node node, s_pathfind path) { List <o_node> nodelist = new List <o_node>(); Vector2Int nodepos = NodePositionFromWorld(node); int xno = nodepos.x, yno = nodepos.y; for (int x = xno + 1; x >= xno - 1; x--) { if (x == xno) { continue; } if (x > 0 && x < gridworldsize.x) { nodelist.Add(nodes[x, yno]); } for (int y = yno + 1; y >= yno - 1; y--) { if (y == yno) { continue; } if (y > 0 && y < gridworldsize.y) { nodelist.Add(nodes[xno, y]); } } } return(nodelist); }
/* * private void OnLevelWasLoaded(int level) * { * if (SceneManager.GetActiveScene().name.ToString() == "InGame") * Initialize(); * }*/ public void Initialize() { ClearGrid(); objPool.Clear(); character_layer = null; block_layer = null; item_layer = null; nodes = null; character_layer = new o_character[gridworldsize.x, gridworldsize.y]; block_layer = new o_tile[gridworldsize.x, gridworldsize.y]; item_layer = new o_item[gridworldsize.x, gridworldsize.y]; nodes = new o_node[gridworldsize.x, gridworldsize.y]; //if (nodes == null) if (GroundSprites == null) { GenerateGround(); } for (int x = 0; x < gridworldsize.x; x++) { for (int y = 0; y < gridworldsize.y; y++) { nodes[x, y] = new o_node(); nodes[x, y].position = new Vector2(nodesize * x, nodesize * y); } } for (int x = 0; x < groundworldsize.x; x++) { for (int y = 0; y < groundworldsize.y; y++) { GroundSprites[x, y].color = Color.white; } } CreateObjectPooler(); FillObjectPooler(); print("Inititalized level!"); if (SceneManager.GetActiveScene().name.ToString() == "InGame") { s_levelloader.load.LoadData(); } //s_charactermanager.SetGameMode(); }
public List <o_node> RetracePath(o_node current, o_node start) { Grid.UnpaintAllNodes(); List <o_node> path = new List <o_node>(); o_node currentnode = current; while (currentnode != start) { path.Add(currentnode); { Vector2Int nodepos = Grid.VectorPositionFromWorld(currentnode.position); //Grid.PaintNode(nodepos, Color.white); HashSet <o_node> neigh = Grid.CheckAroundNode(current); //print(currentnode.DIR); } currentnode = currentnode.parent; } path.Reverse(); /* * print(Grid.ObjectFromWorld(path[0])); * print(Grid.ObjectFromWorld(path[path.Count - 1])); * * if (Grid.ObjectFromWorld(path[path.Count - 1]) != null) * { * s_object obj = Grid.ObjectFromWorld(path[path.Count - 1]); * while (path.Count != 0 && * obj != null && * obj.GetType() == typeof(o_character)) * { * path.Remove(path[path.Count - 1]); * if (path.Count > 0) * if (Grid.ObjectFromWorld(path[path.Count - 1]) != null) * obj = Grid.ObjectFromWorld(path[path.Count - 1]); * else * break; * } * }*/ //path.Add(start); return(path); }
public HashSet <o_node> CheckAroundNode(o_node node) { HashSet <o_node> nodelist = new HashSet <o_node>(); Vector2Int nodepos = NodePositionFromWorld(node); int xno = nodepos.x, yno = nodepos.y; for (int x = xno + 1; x >= xno - 1; x--) { if (x == xno) { continue; } if (x > 0 && x < gridworldsize.x) { nodelist.Add(nodes[x, yno]); } for (int y = yno + 1; y >= yno - 1; y--) { if (y == yno) { continue; } if (y > 0 && y < gridworldsize.y) { /* * if (yno - y == 1) * nodes[xno, y].DIR = o_node.DIRECTION.UP; * * if (yno - y == -1) * nodes[xno, y].DIR = o_node.DIRECTION.DOWN; */ nodelist.Add(nodes[xno, y]); } } } return(nodelist); }
public bool PathFind(Vector2 target, Vector2 goal) { List <o_node> openlist = new List <o_node>(); HashSet <o_node> closelist = new HashSet <o_node>(); o_node startnode = Grid.NodeFromWorld(target); o_node goalnode = Grid.NodeFromWorld(goal); if (goalnode == null || //!startnode.walkable || !goalnode.walkable) { //print("You're not a bloody ghost!"); return(false); } startnode.h = HerusticVal(startnode.position, goal); openlist.Add(startnode); while (openlist.Count > 0) { o_node currentnode = openlist[0]; //print(currentnode.h); for (int i = 1; i < openlist.Count; i++) { if (openlist[i].f <= currentnode.f && openlist[i].h < currentnode.h) { currentnode = openlist[i]; } } openlist.Remove(currentnode); closelist.Add(currentnode); //closednodes.Add(currentnode); //For visual if (currentnode == goalnode) { Grid.ResetNodes(); return(true); } foreach (o_node neighbour in Grid.CheckAroundNode(currentnode, this)) { float newcost = currentnode.g + HerusticVal(currentnode.position, neighbour.position); if (!neighbour.walkable || closelist.Contains(neighbour)) { continue; } if (newcost < neighbour.g || !openlist.Contains(neighbour)) { neighbour.g = newcost; neighbour.h = HerusticVal(neighbour.position, goal); neighbour.parent = currentnode; if (!openlist.Contains(neighbour)) { //opennodes.Add(neighbour); openlist.Add(neighbour); } } } } return(false); }
private void Update() { if (PlayerPrefs.GetInt("MonsterMode") == 0) { if (Players.Count < 10) { PlayerCount.text = "0" + Players.Count; } else { PlayerCount.text = "" + Players.Count; } if (Enemies.Count < 10) { EnemyCount.text = "0" + Enemies.Count; } else { EnemyCount.text = "" + Enemies.Count; } } else { if (Players.Count < 10) { PlayerCount.text = "0" + Enemies.Count; } else { PlayerCount.text = "" + Enemies.Count; } if (Enemies.Count < 10) { EnemyCount.text = "0" + Players.Count; } else { EnemyCount.text = "" + Players.Count; } } Vector3 mousePositon = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (player == null) { pathfcost = 0; stats.text = "Nothing"; } if (GetWinner == VICTORY_DICTATOR.ENEMY) { s_camera.staticCam.StartCoroutine(s_camera.staticCam.Fade(Color.black, 0.3f)); game_on = false; if (s_camera.staticCam.isfaded) { UnityEngine.SceneManagement.SceneManager.LoadScene("Title"); } } if (game_on) { switch (STATES) { case STATEMACHINE.IDLE: switch (TURNSTATE) { case TURNS.PLAYER: if (Input.GetMouseButtonDown(0)) { if (CheckForObject(mousePositon)) { s_object obj = GetObjectFromWorld(mousePositon); if (IsCharacter(mousePositon, obj)) { o_character obselect = obj.GetComponent <o_character>(); if (obselect.health > 0) { if (obselect.playable) { pathfcost = 0; SwitchCharacter((o_character)obj); CheckCharacterSurroundings(false); } player = obselect; STATES = STATEMACHINE.SELECT_CHAR; } } } } break; #region ENEMY case TURNS.ENEMY: if (EnemyQueue.Count > 0) { //print("Chara " + EnemyQueue.Peek()); player = EnemyQueue.Peek(); //print("pl " + player); STATES = STATEMACHINE.SELECT_CHAR; } else { s_camera.staticCam.StartCoroutine(s_camera.staticCam.Fade(Color.clear, 4)); if (s_camera.staticCam.isfaded) { print("Progress END: " + EnemyQueue.Count); EndTurn(); } } break; #endregion } break; case STATEMACHINE.SELECT_CHAR: switch (TURNSTATE) { case TURNS.PLAYER: if (player.current_item != null) { stats.text = player.name + "\n" + "Item: " + player.current_item.name; } else { stats.text = player.name; } //actionpoints = "AP: " + player.actionPoints + "/" + player.maxActionPoints; if (CheckForObject(mousePositon)) { if (Input.GetMouseButtonDown(0)) { CheckCharacterSurroundings(false); s_object obj = GetObjectFromWorld(mousePositon); if (obj != null) { if (obj.GetType() == typeof(o_character)) { o_character chara = obj.GetComponent <o_character>(); if (targets.Contains(chara)) { if (!chara.playable) { targetToAttack = chara; STATES = STATEMACHINE.ATTACK_SELECT; } } } } } } if (Input.GetMouseButtonDown(0)) { Vector2 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (player.MoveCost(mousepos) != -1) { Grid.UnpaintAllNodes(); pathfcost = player.MoveCost(mousepos); player.SetDirections(Grid.NodeFromWorld(mousepos)); List <o_node> dir = player.directions; foreach (o_node d in dir) { Grid.PaintNode(Grid.NodePositionFromWorld(d), Color.yellow); } STATES = STATEMACHINE.MOVE_TO; } } break; #region ENEMY case TURNS.ENEMY: if (player.current_item != null) { UseItem(); } /*if (targetToAttack != null) * if (targetToAttack.health == 0 && !targetToAttack.IsDisappear()) * return; */ CheckCharacterSurroundings(true); if (targets.Count == 0) { STATES = STATEMACHINE.MOVE_TO; } else { STATES = STATEMACHINE.ATTACK_SELECT; } break; #endregion } //Have a cancel button where this player is no longer the focus //Attack button where it checks enemies break; case STATEMACHINE.MOVE_TO: switch (TURNSTATE) { case TURNS.PLAYER: if (Input.GetMouseButtonDown(0)) { Vector2 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (player.playable) { if (player.actionPoints >= pathfcost) { if (Grid.NodeFromWorld(mousepos) == player.directions[player.directions.Count - 1]) { ConfirmMovement(); return; } } } if (player.MoveCost(mousepos) != -1) { Grid.UnpaintAllNodes(); pathfcost = player.MoveCost(mousepos); player.SetDirections(Grid.NodeFromWorld(mousepos)); List <o_node> dir = player.directions; foreach (o_node d in dir) { Grid.PaintNode(Grid.NodePositionFromWorld(d), Color.yellow); } } } if (Input.GetMouseButtonDown(1)) { Grid.UnpaintAllNodes(); LooseFocus(); } break; #region ENEMY case TURNS.ENEMY: int newcost = int.MaxValue; o_character tar = null; foreach (o_character e in Players) { //Check around the target and pathfind for each position. HashSet <o_node> aroundChar = Grid.CheckAroundNode(Grid.NodeFromWorld(e.transform.position)); int potentialMoveCost = int.MaxValue; e.GetComponent <SpriteRenderer>().color = Color.red; o_node nof = null; foreach (o_node no in aroundChar) { if (!no.walkable) { continue; } int comp = player.MoveCost(no.position); if (comp == -1) { continue; } potentialMoveCost = Mathf.Min(potentialMoveCost, comp); if (potentialMoveCost == comp) { nof = no; } } e.GetComponent <SpriteRenderer>().color = Color.white; if (potentialMoveCost <= player.range) { if (potentialMoveCost <= player.actionPoints) { tar = e; newcost = Mathf.Min(newcost, potentialMoveCost); if (newcost == potentialMoveCost) { if (nof != null) { player.SetDirections(nof); } } } } else { continue; } Grid.UnpaintAllNodes(); } if (newcost != int.MaxValue) { Grid.UnpaintAllNodes(); pathfcost = newcost; if (pathfcost > 0) { ConfirmMovement(); } else { NextEnemy(); } } else { NextEnemy(); } break; #endregion } //Confirm? //Cancecl Move break; case STATEMACHINE.ATTACK_SELECT: switch (TURNSTATE) { case TURNS.PLAYER: if (targetToAttack != null) { targetToAttack.renderer.color = Color.magenta; } if (Input.GetMouseButtonDown(0)) { s_object obj = GetObjectFromWorld(mousePositon); if (obj != targetToAttack) { o_character newtarg = targets.Find(x => obj == x); if (newtarg != null) { targetToAttack.renderer.color = Color.white; targetToAttack = newtarg; return; } } } if (targetToAttack.health > 0 && player.actionPoints >= 2) { if (Input.GetMouseButtonDown(0)) { targetToAttack.renderer.color = Color.white; STATES = STATEMACHINE.ATTACK_EXECUTE; } } if (targetToAttack.health <= 0 || player.actionPoints < 2) { targetToAttack.renderer.color = Color.white; LooseFocus(); } if (Input.GetMouseButtonDown(1)) { if (targetToAttack != null) { targetToAttack.renderer.color = Color.white; } LooseFocus(); } break; #region ENEMY case TURNS.ENEMY: CheckCharacterSurroundings(true); if (targets.Count > 0) { //The enemy can attack the player again if they have sufficent points if (player.actionPoints >= 2) { targetToAttack = GetTargetWithLowestHealth(); if (targetToAttack.health > 0) { if (!isattacking && !IsSkipped) { StartCoroutine(AttackingAnim()); } if (IsSkipped) { AttackCalculations(targetToAttack); } } } else { NextEnemy(); } } else { STATES = STATEMACHINE.SELECT_CHAR; } break; #endregion } break; case STATEMACHINE.ATTACK_EXECUTE: if (!isattacking) { StartCoroutine(AttackingAnim()); } else if (TURNSTATE == TURNS.PLAYER) { STATES = STATEMACHINE.ATTACK_SELECT; } break; case STATEMACHINE.WALK: //Call Player enumarator to walk //Once done go back to select char break; } } }
public void SetDirections(o_node goal) { o_node thischaracter = grid.NodeFromWorld(transform.position); directions = path.RetracePath(goal, thischaracter); }
int[,] NodeDivision(o_node nodetarg) { return(new int[((int)nodetarg.position.x + 20 / 2) / 20, ((int)nodetarg.position.y + 20 / 2) / 20]); }