public static bool canTraverseHex(int hex_x, int hex_z, EntityE exclude) { switch(exclude) { case EntityE.Player: if(!isEntityPos(hex_x, hex_z, EntityE.Enemy) && !isEntityPos(hex_x, hex_z, EntityE.Base)) return true; return false; case EntityE.Base: if(!isEntityPos(hex_x, hex_z, EntityE.Enemy) && !isEntityPos(hex_x, hex_z, EntityE.Player)) return true; return false; case EntityE.Enemy: if(!isEntityPos(hex_x, hex_z, EntityE.Base) && !isEntityPos(hex_x, hex_z, EntityE.Player)) return true; return false; default: if(!isEntityPos(hex_x, hex_z, EntityE.Enemy) && !isEntityPos(hex_x, hex_z, EntityE.Base) && !isEntityPos(hex_x, hex_z, EntityE.Player)) return true; return false; } }
public GameObject AddEntity(Vector3 pos, EntityE ent_type, int x, int z) { GameObject new_ent; editorEntityS new_ent_s; print ("ENTITY created @ " + x + ", " + z); if(entity_db.ContainsKey(x)) { if(entity_db[x].ContainsKey(z)) { //if there is already an entity there and we want to overwrite, overwrite it if(entity_db[x][z].getEntityType() != editorUserS.last_created_entity_type) { new_ent = InstantiateEntity(pos, ent_type, x, z); new_ent_s = new_ent.GetComponent<editorEntityS>(); setEntityProperties(new_ent_s); //now delete what is already there Destroy(entity_db[x][z].getEntity()); entity_db[x][z]= new EntityData(new_ent_s.name, new_ent, ent_type, x, z); print ("--replacing : " + entity_db[x][z].getEntityType().ToString() + " with " + ent_type); } else { print ("--same Entity already exists there."); } } else { //we've just gotta make a new z entry since nothing has ever been made in this z spot print ("--nothing ever in that z."); new_ent = InstantiateEntity(pos, ent_type, x, z); new_ent_s = new_ent.GetComponent<editorEntityS>(); setEntityProperties(new_ent_s); entity_db[x].Add(z, new EntityData(new_ent_s.name, new_ent, ent_type, x, z)); } } else { //nothing has ever been made in this x row, so make the z dict and the z entry print ("--nothing ever in that x."); new_ent = InstantiateEntity(pos, ent_type, x, z); new_ent_s = new_ent.GetComponent<editorEntityS>(); setEntityProperties(new_ent_s); entity_db.Add(x, new Dictionary<int, EntityData>()); entity_db[x].Add(z, new EntityData(new_ent_s.name, new_ent, ent_type, x, z)); } return entity_db[x][z].getEntity(); }
//Find path from start node to destination node //Distance function returns distance between two adjacent nodes //Estimate function returns distance between any node and destination node //Neighbors function returns adjacent traversible hexes for given hex input (Very hefty method) public static Path FindPath(HexData start, HexData destination, EntityE entity, Func<HexData, HexData, double> distance, Func<HexData, HexData, double> estimate, Func<HexData, HexData, EntityE, List<HexData>> neighbours) { //set of already checked HexData var closed = new HashSet<HexData>(); //queued HexData in open set var queue = new PriorityQueue<double, Path>(); //start by adding enemy's hex to queue queue.Enqueue(0, new Path (start)); while (!queue.IsEmpty) { //get first element on list var path = queue.Dequeue(); //check to see if this element is in the set of already checked hexes, i.o the closed set if (closed.Contains(path.ThisStep)) continue; //check to see if this element is our destination hex if (path.ThisStep.Equals(destination)) return path; //return full path to destination //if element isn't the destination hex and isn't in closed set, add it to closed set closed.Add(path.ThisStep); //Go through neighbors (adjacent hexes) of current element foreach(HexData n in neighbours(path.ThisStep, destination, entity)) { //compute distance between current element and it's neighbor double d = distance(path.ThisStep, n); //New step added without modifying current path var newPath = path.AddStep(n, d); //add new path to queue queue.Enqueue(newPath.TotalCost + estimate(n, destination), newPath); } } return null; }
//Same has other canTraversHex except check to see if the indicated hex can be moved onto without considering excluded entity into calculations //assumption is that if you send in "null" you will get to default case otherwise send it NODE public static bool canTraverseHex(HexData hex, EntityE exclude) { return canTraverseHex(hex.x, hex.z, exclude); }
//instantiate an object into the gamespace private static GameObject instantiateEntity(int x, int z, EntityE entity_type) { return (GameObject) Instantiate(entity_dict[entity_type], CoordsGameTo3DEntiy(x, z), Quaternion.identity); }
public static bool isEntityPos(int hex_x, int hex_z, EntityE entity) { switch(entity) { case EntityE.Player: if(hex_x == mech_s.x && hex_z == mech_s.z) return true; return false; case EntityE.Base: if(hex_x == base_s.x && hex_z == base_s.z) return true; return false; case EntityE.Node: if(resource_node_list != null){ foreach(entityNodeS node in resource_node_list) if(hex_x == node.x && hex_z == node.z) return true; } return false; case EntityE.Enemy: if(enemy_list != null){ foreach(entityEnemyS enemy in enemy_list) if(hex_x == enemy.x && hex_z == enemy.z) return true; } return false; default: return false; } }
public static bool isEntityPos(HexData hex, EntityE entity) { return isEntityPos(hex.x, hex.z, entity); }
//Get path to base public static Path getTraversablePath(HexData start, HexData destination, EntityE ignore_entity, Func<HexData, HexData, double> traversal_cost_func, Func<HexData, HexData, EntityE, List<HexData>> neighbor_hex_func) { //Send hex of base and hex of enemy to aStar var path = aStar.FindPath(start, destination, ignore_entity, traversal_cost_func, calcCostToDestinationHex, neighbor_hex_func); // if(path != null){ // last_path_cost = path.TotalCost; // Debug.Log ("getTraversablePath: path cost = " + last_path_cost); // } return path; }
public HexData(int _x, int _z, bool THIS_METHOD_IS_FOR_DUMMY_PERIMETER_HEXES_ONLY) { x = _x; z = _z; hex_type = Hex.Perimeter; direction_from_central_hex = Facing.South; added_occupier = EntityE.NotCheckedYet; traversal_cost = -1; hex_object = null; vision_state = Vision.Unvisted; hex_script = null; is_node_here = false; }
private GameObject InstantiateEntity(Vector3 pos, EntityE ent_type, int x, int z) { GameObject new_ent = (GameObject) Instantiate(entity_dict[ent_type], pos, Quaternion.identity); editorEntityS ent_s = new_ent.GetComponent<editorEntityS>(); ent_s.x_coord = x; ent_s.z_coord = z; ent_s.name = "entity("+ent_s.x_coord +"," + ent_s.z_coord+")"; ent_s.entity_type = ent_type; return new_ent; }
public void LoadEntity(EntityE ent_type, int x, int z) { Vector3 converted = editorUserS.CoordsGameTo3D(x, z); Vector3 adjusted = new Vector3(converted.x, converted.y + 1F, converted.z + .5F); AddEntity(adjusted, ent_type, x, z); }
public bool canTraverse(int hex_x, int hex_z, EntityE entity) { HexData hex = hexManagerS.getHex(hex_x, hex_z); if(!entityManagerS.canTraverseHex(hex.x, hex.z, entity)){ //Debug.Log ("enemy hex"); return false; }else if(hex.hex_type == Hex.Water || hex.hex_type == Hex.Mountain || hex.hex_type == Hex.Perimeter){ //Debug.Log ("enviro hex"); return false; }else return true; }
//alternative can traverse methods where an entity can be excluded from search public bool canTraverse(HexData hex, EntityE entity) { return canTraverse(hex.x, hex.z, entity); }
//True if enemy can can get to specified opponent bool canGetToOpponent(HexData enemy, HexData opponent, EntityE entity_opponent, bool knows_opponents_location, ref List<HexData> path_to_opp) { if(knows_opponents_location){ //if enemy already "knows opponents location" then don't worry about visibility just get path to opponent Debug.Log ("canGetToOpponent: knows enemy location, get path:" + entity_opponent); path_to_opp = extractPath(hexManagerS.getTraversablePath(enemy, opponent, EntityE.Node, getTraverseAPCostPathVersion, getAdjacentTraversableHexes)); if(path_to_opp.Count == 0){ Debug.Log ("canGetToOpponent: knows enemy location, but can't find path"); return false; }else{ Debug.Log ("canGetToOpponent: knows enemy location, and found path"); return true; } } else { Debug.Log ("canGetToOpponent: doesn't know enemy location, see if enemy is visible in sight range: " + entity_opponent); //TODO: TESTING****using old method********* if(canSeeHex(opponent)){ //if(canSeeHex(opponent)){ **** Debug.Log ("canGetToOpponent: doesn't know enemy location, get path to " + entity_opponent); path_to_opp = extractPath(hexManagerS.getTraversablePath(enemy, opponent, EntityE.Node, getTraverseAPCostPathVersion, getAdjacentTraversableHexes)); if(path_to_opp.Count == 0){ Debug.Log ("canGetToOpponent: enemy is visible, but can't find path"); return false; }else{ Debug.Log ("canGetToOpponent: enemy is visible, and found path"); return true; } }else{ Debug.Log ("canGetToOpponent: doesn't know enemy location, and enemy not visible"); return false; } } }
void EntityUpdate() { GameObject clicked_game_object; editorEntityS entity_s; //alt + left click if(Input.GetKey(KeyCode.LeftAlt) && Input.GetMouseButtonDown(0)) { clicked_game_object = RaycastMouse(EntTag); if(clicked_game_object != null) { entity_s = clicked_game_object.GetComponent<editorEntityS>(); editorUserS.last_created_entity_type = entity_s.entity_type; } } else //ctrl + left click if(Input.GetKey(KeyCode.LeftControl) && Input.GetMouseButtonDown(0)) { clicked_game_object = RaycastMouse(EntTag); if(clicked_game_object != null) { entity_s = clicked_game_object.GetComponent<editorEntityS>(); ems.deleteEntity(entity_s); } } else //just left click if(Input.GetMouseButtonDown(0)) { clicked_game_object = RaycastMouse(EntTag); if(clicked_game_object == null) { clicked_game_object = RaycastMouse(HexTag); editorHexS hex_script = clicked_game_object.GetComponent<editorHexS>(); editorUserS.ems.AddEntity( new Vector3(clicked_game_object.transform.position.x, clicked_game_object.transform.position.y + 1F , clicked_game_object.transform.position.z + .5F), last_created_entity_type, hex_script.x_coord, hex_script.z_coord); } } else //right click if(Input.GetMouseButton(1) && !selection_menu_displayed) { Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5)); p.y = 5; terrain_menu = (GameObject) Instantiate(entMenuPrefab, p, Quaternion.identity); selection_menu_displayed = true; print ("displaying menu"); } else if(!Input.GetMouseButton(1) && selection_menu_displayed) { selection_menu_displayed = false; clicked_game_object = RaycastMouse(EntTag); if(clicked_game_object != null) { entity_s = clicked_game_object.GetComponent<editorEntityS>(); if(entity_s.menu_item) { clicked_game_object = RaycastMouse(EntTag); entity_s = clicked_game_object.GetComponent<editorEntityS>(); editorUserS.last_created_entity_type = entity_s.entity_type; } } Destroy(terrain_menu); terrain_menu = null; print ("destroying menu"); } }
public void PersistAndFind() { { var ec1 = new EntityC(); TestBase.TestSerialization(ec1); var ey = new EntityY(); var ec2 = new EntityC { X = new[] { new EntityX(), new EntityX() }, Y = new[] { ey, ey }, Y2 = ey }; TestBase.TestSerialization(ec2); using (var em = Emf.CreateEntityManager()) { em.Persist(ec1); Assert.IsFalse(string.IsNullOrEmpty(ec1.Id)); em.Persist(ec2); Assert.IsFalse(string.IsNullOrEmpty(ec2.Id)); } using (var em = Emf.CreateEntityManager()) { var _ec1 = em.Find <EntityC>(ec1.Id); Assert.AreEqual(ec1, _ec1); var _ec2 = em.Find <EntityC>(ec2.Id); Assert.AreEqual(ec2, _ec2); Assert.AreSame(_ec2.Y[0], _ec2.Y[1]); Assert.AreSame(_ec2.Y2, _ec2.Y[0]); } } { var ed1 = new EntityD(); TestBase.TestSerialization(ed1); var ey = new EntityY(); var ed2 = new EntityD { X = new HashSet <EntityX>(new[] { new EntityX(), new EntityX() }), Y = new HashSet <EntityY>(new[] { ey, new EntityY() }), Y2 = ey }; TestBase.TestSerialization(ed2); using (var em = Emf.CreateEntityManager()) { em.Persist(ed1); Assert.IsFalse(string.IsNullOrEmpty(ed1.Id)); em.Persist(ed2); Assert.IsFalse(string.IsNullOrEmpty(ed2.Id)); } using (var em = Emf.CreateEntityManager()) { var _ed1 = em.Find <EntityD>(ed1.Id); Assert.AreEqual(ed1, _ed1); var _ed2 = em.Find <EntityD>(ed2.Id); Assert.AreEqual(ed2, _ed2); Assert.IsNotNull(_ed2.Y.FirstOrDefault(item => object.ReferenceEquals(item, _ed2.Y2))); } } { var ee1 = new EntityE(); TestBase.TestSerialization(ee1); var ey = new EntityY(); var ee2 = new EntityE { X = new List <EntityX>(new[] { new EntityX(), new EntityX() }), Y = new List <EntityY>(new[] { ey, ey }), Y2 = ey }; TestBase.TestSerialization(ee2); using (var em = Emf.CreateEntityManager()) { em.Persist(ee1); Assert.IsFalse(string.IsNullOrEmpty(ee1.Id)); em.Persist(ee2); Assert.IsFalse(string.IsNullOrEmpty(ee2.Id)); } using (var em = Emf.CreateEntityManager()) { var _ee1 = em.Find <EntityE>(ee1.Id); Assert.AreEqual(ee1, _ee1); var _ee2 = em.Find <EntityE>(ee2.Id); Assert.AreEqual(ee2, _ee2); Assert.AreSame(_ee2.Y[0], _ee2.Y[1]); Assert.AreSame(_ee2.Y2, _ee2.Y[0]); } } { var ef1 = new EntityF(); TestBase.TestSerialization(ef1); var ey = new EntityY(); var ef2 = new EntityF { X = new ArrayList(new[] { new EntityX(), new EntityX() }), Y = new ArrayList(new[] { ey, ey }), Y2 = ey }; TestBase.TestSerialization(ef2); using (var em = Emf.CreateEntityManager()) { em.Persist(ef1); Assert.IsFalse(string.IsNullOrEmpty(ef1.Id)); em.Persist(ef2); Assert.IsFalse(string.IsNullOrEmpty(ef2.Id)); } using (var em = Emf.CreateEntityManager()) { var _ef1 = em.Find <EntityF>(ef1.Id); Assert.AreEqual(ef1, _ef1); var _ef2 = em.Find <EntityF>(ef2.Id); Assert.AreEqual(ef2, _ef2); Assert.AreSame(_ef2.Y[0], _ef2.Y[1]); Assert.AreSame(_ef2.Y2, _ef2.Y[0]); } } }
public HexData(int _x, int _z, Hex _type, GameObject _hex_object, engineHexS _hex_script, Vision _vision) { x = _x; z = _z; hex_type = _type; hex_object = _hex_object; vision_state = _vision; direction_from_central_hex = Facing.South; //this is a temp value, should be replaced manually usually added_occupier = EntityE.NotCheckedYet; traversal_cost = -1; hex_script = _hex_script; is_node_here = false; }
public EntityData(string _name, GameObject _occupier, EntityE _entity_type, int _x_coord, int _z_coord) { name = _name; x_coord = _x_coord; z_coord = _z_coord; occupier = _occupier; entity_type = _entity_type; }
public List<HexData> getAdjacentTraversableHexesPathVersion(HexData hex, HexData destination, EntityE entity) { List<HexData> result_hexes = new List<HexData>(); //hold resulting hexes //Get adjacent tiles around player mech HexData[] adjacent_hexes = hexManagerS.getAdjacentHexes(hex.x, hex.z); //Debug.Log(adjacent_hexes.Length + " found adjacent"); //See which of the adjacent hexes are traversable for(int i = 0; i < adjacent_hexes.Length; i++){ if(entityManagerS.isEntityPos(adjacent_hexes[i], entity) || (canTraverse(adjacent_hexes[i]) && adjacent_hexes[i].vision_state != Vision.Unvisted)) { //add hex to traversable array result_hexes.Add(adjacent_hexes[i]); } } //Debug.Log ("Number of result_hexes " + result_hexes.Count); return result_hexes; // return getAdjacentTraversableHexes(); }
public void PersistAndFind() { { var ec1 = new EntityC(); TestBase.TestSerialization(ec1); var ey = new EntityY(); var ec2 = new EntityC { X = new[] { new EntityX(), new EntityX() }, Y = new[] { ey, ey }, Y2 = ey }; TestBase.TestSerialization(ec2); using (var em = Emf.CreateEntityManager()) { em.Persist(ec1); Assert.IsFalse(string.IsNullOrEmpty(ec1.Id)); em.Persist(ec2); Assert.IsFalse(string.IsNullOrEmpty(ec2.Id)); } using (var em = Emf.CreateEntityManager()) { var _ec1 = em.Find<EntityC>(ec1.Id); Assert.AreEqual(ec1, _ec1); var _ec2 = em.Find<EntityC>(ec2.Id); Assert.AreEqual(ec2, _ec2); Assert.AreSame(_ec2.Y[0], _ec2.Y[1]); Assert.AreSame(_ec2.Y2, _ec2.Y[0]); } } { var ed1 = new EntityD(); TestBase.TestSerialization(ed1); var ey = new EntityY(); var ed2 = new EntityD { X = new HashSet<EntityX>(new[] { new EntityX(), new EntityX() }), Y = new HashSet<EntityY>(new[] { ey, new EntityY() }), Y2 = ey }; TestBase.TestSerialization(ed2); using (var em = Emf.CreateEntityManager()) { em.Persist(ed1); Assert.IsFalse(string.IsNullOrEmpty(ed1.Id)); em.Persist(ed2); Assert.IsFalse(string.IsNullOrEmpty(ed2.Id)); } using (var em = Emf.CreateEntityManager()) { var _ed1 = em.Find<EntityD>(ed1.Id); Assert.AreEqual(ed1, _ed1); var _ed2 = em.Find<EntityD>(ed2.Id); Assert.AreEqual(ed2, _ed2); Assert.IsNotNull(_ed2.Y.FirstOrDefault(item => object.ReferenceEquals(item, _ed2.Y2))); } } { var ee1 = new EntityE(); TestBase.TestSerialization(ee1); var ey = new EntityY(); var ee2 = new EntityE { X = new List<EntityX>(new[] { new EntityX(), new EntityX() }), Y = new List<EntityY>(new[] { ey, ey }), Y2 = ey }; TestBase.TestSerialization(ee2); using (var em = Emf.CreateEntityManager()) { em.Persist(ee1); Assert.IsFalse(string.IsNullOrEmpty(ee1.Id)); em.Persist(ee2); Assert.IsFalse(string.IsNullOrEmpty(ee2.Id)); } using (var em = Emf.CreateEntityManager()) { var _ee1 = em.Find<EntityE>(ee1.Id); Assert.AreEqual(ee1, _ee1); var _ee2 = em.Find<EntityE>(ee2.Id); Assert.AreEqual(ee2, _ee2); Assert.AreSame(_ee2.Y[0], _ee2.Y[1]); Assert.AreSame(_ee2.Y2, _ee2.Y[0]); } } { var ef1 = new EntityF(); TestBase.TestSerialization(ef1); var ey = new EntityY(); var ef2 = new EntityF { X = new ArrayList(new[] { new EntityX(), new EntityX() }), Y = new ArrayList(new[] { ey, ey }), Y2 = ey }; TestBase.TestSerialization(ef2); using (var em = Emf.CreateEntityManager()) { em.Persist(ef1); Assert.IsFalse(string.IsNullOrEmpty(ef1.Id)); em.Persist(ef2); Assert.IsFalse(string.IsNullOrEmpty(ef2.Id)); } using (var em = Emf.CreateEntityManager()) { var _ef1 = em.Find<EntityF>(ef1.Id); Assert.AreEqual(ef1, _ef1); var _ef2 = em.Find<EntityF>(ef2.Id); Assert.AreEqual(ef2, _ef2); Assert.AreSame(_ef2.Y[0], _ef2.Y[1]); Assert.AreSame(_ef2.Y2, _ef2.Y[0]); } } }