public Vector3 getNextPosition(GameObject obj, bool join) { GOProperties gop = (GOProperties)obj.GetComponent(typeof(GOProperties)); int index = objectTypeExists(gop.type); printTileText("Adding", gop); if (index != -1 && join) { printTileText("Already represented", gop); return(separatePositions [objPos [index]]); } else { objPos.Add(gop.UniqueID, getFirstAvailablePos()); objsOnTile.Add(obj); printTileText("New position " + objPos [gop.UniqueID], gop); if (objsOnTile.Count == maxSpaces - 1) { //increase number of spaces on tile maxSpaces += incrementAmount; createSeparatePositions(); foreach (GameObject o in objsOnTile) { GOProperties gopTemp = (GOProperties)o.GetComponent(typeof(GOProperties)); o.transform.position = separatePositions [objPos [gopTemp.UniqueID]] + new Vector3(0f, 0.3f, 0f); } } Vector3 result = separatePositions [objPos [gop.UniqueID]]; return(result); } }
public void removeObjectFromTile(int uniqueId) { GOProperties gop = (GOProperties)GridManager.instance.gameobjects [uniqueId].GetComponent(typeof(GOProperties)); printTileText("Deleting", gop); if (objPos.ContainsKey(uniqueId)) { int posToDelete = objPos [uniqueId]; objsOnTile.RemoveAt(posToDelete); objPos.Remove(uniqueId); //sync position of other objects after deletion List <int> keys = new List <int> (objPos.Keys); foreach (int key in keys) { if (posToDelete < objPos[key]) { objPos [key] -= 1; } } } else { printTileText("Failed to delete properly", gop); } }
public int getPlayerOwner() { if (objsOnTile.Count > 0) { GOProperties gop = (GOProperties)objsOnTile [0].GetComponent(typeof(GOProperties)); return(gop.PlayerId); } return(-1); }
//Method used to switch destination and origin tiles after the destination is reached void switchOriginAndDestinationTiles() { GOProperties gop = (GOProperties)this.GetComponent(typeof(GOProperties)); GridManager GM = GridManager.instance; GM.DestroyPath(gop.UniqueID); GM.DestroyTilesPath(gop.UniqueID); GM.getOriginTileTB() [gop.UniqueID] = GM.destTileTB [gop.UniqueID]; GM.destTileTB [gop.UniqueID] = null; }
public int objectTypeExists(string type) { foreach (GameObject objOnTile in objsOnTile) { GOProperties gop = (GOProperties)objOnTile.GetComponent(typeof(GOProperties)); if (gop.type == type) { return(gop.UniqueID); } } return(-1); }
public int getFanaticsOnTile() { int f = 0; foreach (GameObject objOnTile in objsOnTile) { GOProperties gop = (GOProperties)objOnTile.GetComponent(typeof(GOProperties)); if (gop.type.Equals("ThirdPersonController")) { f += gop.Quantity; } } return(f); }
//changes back to fully transparent material when mouse cursor is no longer hovering over the tile void OnMouseExit() { foreach (GameObject unit in GridManager.unitSelected) { if (unit != null) { GOProperties gop = (GOProperties)unit.GetComponent(typeof(GOProperties)); GridManager.instance.selectedTile = null; if (tile.Passable && this != GridManager.instance.destTileTB [gop.UniqueID] && this != GridManager.instance.getOriginTileTB() [gop.UniqueID]) { changeColor(Color.white); } } } }
bool isEnemyOnTile() { Debug.Log("checking isEnemyOnTile??"); GOProperties gop1 = (GOProperties)this.GetComponent(typeof(GOProperties)); Collider[] collisions = Physics.OverlapSphere(transform.position, 5f); foreach (Collider col in collisions) { if (col.gameObject.tag == "Unit") { GOProperties gop2 = (GOProperties)col.gameObject.GetComponent(typeof(GOProperties)); if (gop1.PlayerId != gop2.PlayerId) { return(true); } } } return(false); }
//IMPORTANT: for methods like OnMouseEnter, OnMouseExit and so on to work, collider (Component -> Physics -> Mesh Collider) should be attached to the prefab void OnMouseEnter() { if (!EventSystem.current.IsPointerOverGameObject()) { foreach (GameObject unit in GridManager.unitSelected) { if (unit != null) { GOProperties gop = (GOProperties)unit.GetComponent(typeof(GOProperties)); GridManager.instance.selectedTile = tile; //when mouse is over some tile, the tile is passable and the current tile is neither destination nor origin tile, change color to orange if (tile.Passable && this != GridManager.instance.destTileTB [gop.UniqueID] && this != GridManager.instance.getOriginTileTB() [gop.UniqueID]) { changeColor(orange); } } } } }
void destTileChanged() { foreach (GameObject unit in GridManager.unitSelected) { if (unit != null) { GOProperties gop = (GOProperties)unit.GetComponent(typeof(GOProperties)); //var destTile = GridManager.instance.destTileTB; //deselect destination tile if user clicks on current destination tile //if (this == destTile) //{ //GridManager.instance.destTileTB = null; //GetComponent<Renderer>().material.color = temptilecolour; //return; //} //if there was other tile marked as destination, change its material to default (fully transparent) one //if (destTile != null) // destTile.GetComponent<Renderer>().material = defaultMaterial; GridManager.instance.destTileTB [gop.UniqueID] = this; //changeColor(Color.green); } } }
//called every frame when mouse cursor is on this tile void OnMouseOver() { if (!EventSystem.current.IsPointerOverGameObject()) { foreach (GameObject unit in GridManager.unitSelected) { if (unit != null) { GOProperties gop = (GOProperties)unit.GetComponent(typeof(GOProperties)); //if player right-clicks on the tile, toggle passable variable and change the color accordingly // if (Input.GetMouseButtonUp(1)) // { // } //if user left-clicks the tile bool moving = GridManager.instance.isAnyMoving(); if (Input.GetMouseButtonUp(0) & unit != null & moving == false) { if (tile.Passable) { changeColor(Color.white); TileBehaviour originTileTB = GridManager.instance.getOriginTileTB() [gop.UniqueID]; //if user clicks on origin tile or origin tile is not assigned yet if (this == originTileTB || originTileTB == null) { originTileChanged(); } else { destTileChanged(); } GridManager.instance.generateAndShowPath(); } } } } } }
public void generateItems(int startLevel = -1) { prefabPool = new Dictionary <string, GameObject>(); sceneItemsList = new List <GOProperties>(); // if the XmlItems gameobject folder remained in the Hierarcy, then delete it while (GameObject.Find(xmlItemsGameObjectName) != null) { MonoBehaviour.DestroyImmediate(GameObject.Find(xmlItemsGameObjectName)); } parentOfXmlItems = new GameObject(xmlItemsGameObjectName).transform; parentOfXmlItems.parent = GameObject.Find("GameContext").transform; deserializedLevels = XmlIO.LoadXml <DeserializedLevels>("Levels"); // if startlevel is in the XML i.e. <Developer StartLevel="3" /> then get level from there // otherwise start with level 1 if (startLevel == -1) { startLevel = int.Parse(deserializedLevels.developer.startLevel); } DeserializedLevels.Level currentLevel = deserializedLevels.levels[startLevel - 1]; // spagetti coding set player, camera position and level bounds setPos2D( GameObject.Find("PlayerView"), new Vector2(toFloatZeroIfNull(currentLevel.playerx), toFloatZeroIfNull(currentLevel.playery))); setPos2D( GameObject.Find("CameraView"), new Vector2(toFloatZeroIfNull(currentLevel.playerx), toFloatZeroIfNull(currentLevel.playery))); // set level's bounds #if UNITY_EDITOR try { #endif setLevelBounds( parentOfXmlItems, float.Parse(currentLevel.bound_x_min), float.Parse(currentLevel.bound_y_min), float.Parse(currentLevel.bound_x_max), float.Parse(currentLevel.bound_y_max)); #if UNITY_EDITOR } catch { Debug.LogError("Level Bounds must be set in levels.xml"); } #endif // <Item prefab="Chair" x="1" y="10" rot="90" /> foreach (DeserializedLevels.Item deserializedItem in currentLevel.items) { // caching prefabString i.e. "phone" string prefabString = deserializedItem.prefab; // if the prefab in the item XmlNode has not been loaded then add it to the prefabsDict dictionary, if (!prefabPool.ContainsKey(prefabString)) { // load prefab GameObject prefabObject = Resources.Load(prefabsFolder + prefabString, typeof(GameObject)) as GameObject; // if unsuccesful, error message and jump to next in the foreach loop if (prefabObject == null) { Debug.LogError("Prefab \"" + prefabString + "\" does not exists."); continue; } // otherwise add to dictionary prefabPool.Add(prefabString, prefabObject); } GOProperties item = new GOProperties(); item.prefab = prefabPool[prefabString]; item.x = toFloatZeroIfNull(deserializedItem.x); item.y = toFloatZeroIfNull(deserializedItem.y); item.rot = toFloatZeroIfNull(deserializedItem.rot); item.scale_x = toFloatOneIfNull(deserializedItem.scale_x); item.scale_y = toFloatOneIfNull(deserializedItem.scale_y); item.speed = toFloatOneIfNull(deserializedItem.speed); item.spotmedianangle = toFloatZeroIfNull(deserializedItem.spotmedianangle); sceneItemsList.Add(item); } // Finally instantiate all items foreach (GOProperties item in sceneItemsList) { // TODO load height coordinate from a directory GameObject newGameObject = MonoBehaviour.Instantiate(item.prefab) as GameObject; // set position setPos2D(newGameObject, new Vector2(item.x, item.y)); // set rotation setRot2D(newGameObject, item.rot); // set scale newGameObject.transform.localScale = new Vector3(item.scale_x, item.scale_y, 1); // set speed if (newGameObject.GetComponent <ShipPatrolView>() != null) { newGameObject.GetComponent <ShipPatrolView>().speed = item.speed; } if (newGameObject.GetComponent <SoldierWalkingView>() != null) { newGameObject.GetComponent <SoldierWalkingView>().speed = item.speed; } // set spotlight median angle if (newGameObject.GetComponentInChildren <SpotLightView>() != null) { newGameObject.GetComponentInChildren <SpotLightView>().spotMedianAngle = item.spotmedianangle; } // set parent newGameObject.transform.parent = parentOfXmlItems; } }
void Update() { GridManager GM = GridManager.instance; if (unitInterval >= GM.globalInterval) { m_character.Move(Vector3.zero, false, false); return; } else if (waiting) { waiting = false; if (isEnemyOnTile()) { Debug.Log("ENEMY ON TILE!!"); path = new List <Tile>(); path.Add(previousTile); GOProperties gop = (GOProperties)this.GetComponent(typeof(GOProperties)); GM.destTileTB [gop.UniqueID] = GM.board [previousTile.boardCoords]; closeToDest = null; curTile = previousTile; } } if (!IsMoving) { m_character.Move(Vector3.zero, false, false); return; } if (closeToDest == null) { GOProperties gop = (GOProperties)this.GetComponent(typeof(GOProperties)); closeToDest = GM.destTileTB [gop.UniqueID].getNextPosition(this.gameObject); } if (path.IndexOf(curTile) == 0) { curTilePos = closeToDest.Value; curTilePos.y = myTransform.position.y; } //if the distance between the character and the center of the next tile is short enough if ((curTilePos - myTransform.position).sqrMagnitude < MinNextTileDist * MinNextTileDist) { unitInterval++; waiting = true; //set custom destinitation //if we reached the destination tile if (path.IndexOf(curTile) == 0) { IsMoving = false; unitInterval = 0; switchOriginAndDestinationTiles(); closeToDest = null; previousTile = curTile; return; } //curTile becomes the next one previousTile = curTile; curTile = path [path.IndexOf(curTile) - 1]; curTilePos = calcTilePos(curTile); } MoveTowards(curTilePos); }
private void printTileText(string text, GOProperties gop) { Debug.Log(text + " id " + gop.UniqueID + "(" + gop.type + ", team " + gop.PlayerId + ") on tile (" + this.tile.boardCoords.X + "," + this.tile.boardCoords.Y + ")"); }