public void SetData(BaseItemScript baseItem) { this._baseItem = baseItem; this._productionRate = baseItem.itemData.configuration.productionRate; this._productType = baseItem.itemData.configuration.product; this._lastCollectedTime = Time.realtimeSinceStartup; }
private IEnumerator _StartTraining() { ItemsCollection.ItemData itemData = Items.GetItem(_currentTrainingTroop); float buildTime = itemData.configuration.buildTime; trainingStartTime = Time.realtimeSinceStartup; yield return(new WaitForSeconds(buildTime)); Vector3 randomFrontCell = _baseItem.GetRandomFrontCellPosition(); BaseItemScript newUnit = SceneManager.instance.AddItem(_currentTrainingTroop, -1, (int)randomFrontCell.x, (int)randomFrontCell.y, true, true); BaseItemScript nearestArmyCamp = SceneManager.instance.GetNearestArmyCamp(newUnit.GetPosition()); if (nearestArmyCamp != null) { newUnit.WalkRandom(nearestArmyCamp); } _currentTrainingTroop = -1; trainingQueue.RemoveAt(0); if (trainingQueue.Count > 0) { _Train(trainingQueue[0]); } if (TrainTroopsWindowScript.instance != null) { TrainTroopsWindowScript.instance.RenderTrainingQueue(); } }
public void OnClick() { //this.GetComponentInParent<ShopWindowScript> ().OnClickCategory (this._category); int itemId = 0; switch (this._category) { case ShopWindowScript.Category.ARMY: itemId = 3823; break; case ShopWindowScript.Category.DECORATIONS: itemId = 8833; break; case ShopWindowScript.Category.DEFENCE: itemId = 6871; break; } //ItemsCollection.ItemData itemData = Items.GetItem (itemId); //Vector3 freePosition = GroundManager.instance.GetRandomFreePositionForItem (itemData.gridSize, itemData.gridSize); BaseItemScript item = SceneManager.instance.AddItem(itemId, false, true); //item.SetPosition (freePosition); if (item != null) { DataBaseManager.instance.UpdateItemData(item); } this.GetComponentInParent <ShopWindowScript> ().Close(); }
public void UpdateAllNodes() { this.instanceNodes = new int[nodeWidth, nodeHeight]; this.pathNodesWithoutWall = new bool[nodeWidth, nodeHeight]; this.pathNodesWithWall = new bool[nodeWidth, nodeHeight]; for (int x = startX; x < nodeWidth; x++) { for (int y = startY; y < nodeHeight; y++) { this.instanceNodes [x, y] = -1; this.pathNodesWithoutWall [x, y] = true; this.pathNodesWithWall [x, y] = true; } } foreach (KeyValuePair <int, BaseItemScript> entry in SceneManager.instance.GetItemInstances()) { BaseItemScript item = entry.Value; if (!item.itemData.configuration.isCharacter) { this.UpdateBaseItemNodes(item, Action.ADD); } } }
/* WALL FUNCTIONS */ /* if the item is a wall */ public void UpdateWall() { return; bool hasRighNeighbourWall = false; bool hasLeftNeighbourWall = false; bool hasTopNeighbourWall = false; bool hasBottomNeighbourWall = false; //check item in right BaseItemScript item = GroundManager.instance.GetItemInPosition(this.GetPosition() + new Vector3(1, 0, 0)); if (item != null && item.itemData.name == "Wall") { hasRighNeighbourWall = true; } //check item in left item = GroundManager.instance.GetItemInPosition(this.GetPosition() + new Vector3(-1, 0, 0)); if (item != null && item.itemData.name == "Wall") { hasLeftNeighbourWall = true; } //check item in top item = GroundManager.instance.GetItemInPosition(this.GetPosition() + new Vector3(0, 0, 1)); if (item != null && item.itemData.name == "Wall") { hasTopNeighbourWall = true; } //check item in bottom item = GroundManager.instance.GetItemInPosition(this.GetPosition() + new Vector3(0, 0, -1)); if (item != null && item.itemData.name == "Wall") { hasBottomNeighbourWall = true; } int frame = 0; if (hasTopNeighbourWall && hasRighNeighbourWall) { frame = 3; } else if (hasRighNeighbourWall) { frame = 1; } else if (hasTopNeighbourWall) { frame = 2; } else { frame = 0; } this.Renderer.GetRenderQuads()[0].GetComponent <TextureSheetAnimationScript>().SetFrame(frame); }
public void OnEnemyItemDestroy(BaseItemScript item) { bool isEverythingDestroyed = true; foreach (KeyValuePair <int, BaseItemScript> entry in this._itemInstances) { BaseItemScript baseItem = entry.Value; if (!baseItem.itemData.configuration.isCharacter && baseItem.itemData.name != "Wall") { //item is not character and not a wall //check the item is destroyed or not, if not then everything in the city is not destroyed if (!baseItem.isDestroyed) { isEverythingDestroyed = false; } } } if (isEverythingDestroyed) { //war ends AttackOverlayWindowScript.instance.Close(); UIManager.instance.ShowResultWindow(true, _swordManExpended, _archerExpended); } }
public void WalkRandom(BaseItemScript parentItem) { if (!this.itemData.configuration.isCharacter) { return; } this._randomWalkParentItem = parentItem; Vector3 pos = Vector3.zero; if (parentItem == null) { Vector3 randomFreePosition = GroundManager.instance.GetRandomFreePosition(); pos = randomFreePosition; } else { int posX = parentItem.GetPositionX(); int posZ = parentItem.GetPositionZ(); int sizeX = (int)parentItem.GetSize().x; int sizeZ = (int)parentItem.GetSize().z; pos.x = Random.Range(posX, posX + sizeX); pos.z = Random.Range(posZ, posZ + sizeZ); } this.Walker.WalkToPosition(pos); this.Walker.OnFinishWalk += this.WalkRandomLoop; }
public BaseItemScript GetNearestArmyCamp(Vector3 from) { BaseItemScript[] armyCamps = this.GetArmyCamps(); if (armyCamps.Length == 0) { return(null); } if (armyCamps.Length == 1) { return(armyCamps[0]); } float smallDistance = 999999; BaseItemScript nearestArmyCamp = null; foreach (BaseItemScript armyCamp in armyCamps) { float dist = Vector3.Distance(armyCamp.GetPosition(), from); if (dist < smallDistance) { smallDistance = dist; nearestArmyCamp = armyCamp; } } return(nearestArmyCamp); }
public void UpdateBaseItemMove() { if (Input.GetMouseButtonDown(0)) { this._tapItemStartPos = this._TryGetRaycastHitBaseGround(Input.mousePosition); this._tapStartRaycastedItem = this._TryGetRaycastHitBaseItem(Input.mousePosition); this._isDraggingBaseItem = false; this._isDragItemStarted = false; } if (Input.GetMouseButton(0) && this._tapItemStartPos != positiveInfinityVector) { if (this._isTappedBaseItem && this._selectedBaseItem == this._tapStartRaycastedItem) { Vector3 currentTapPosition = this._TryGetRaycastHitBaseGround(Input.mousePosition); if (Vector3.Distance(this._tapItemStartPos, currentTapPosition) >= _minimumMoveDistanceForItemMove) { CameraEvent evt = new CameraEvent() { point = currentTapPosition, baseItem = this._selectedBaseItem }; if (!this._isDragItemStarted) { // Debug.Log ("BaseItemDragStart"); this._isDragItemStarted = true; if (this.OnItemDragStart != null) { this.OnItemDragStart.Invoke(evt); } } // Debug.Log ("BaseItemDrag"); this._isDraggingBaseItem = true; if (this.OnItemDrag != null) { this.OnItemDrag.Invoke(evt); } } } } if (Input.GetMouseButtonUp(0)) { this._tapItemStartPos = positiveInfinityVector; if (_isDragItemStarted) { // Debug.Log ("BaseItemDragStop"); this._isDragItemStarted = false; this._isDraggingBaseItem = false; if (this.OnItemDragStop != null) { this.OnItemDragStop.Invoke(null); } } } }
/// <summary> /// Removes the item. /// </summary> /// <param name="item">Item.</param> public void RemoveItem(BaseItemScript item) { this._itemInstances.Remove(item.instanceId); if (item != null) { Destroy(item.gameObject); } }
/// <summary> /// Adds the item with itemId. where itemId is the id which we registered with item prefab as unique. /// </summary> /// <returns>The item.</returns> /// <param name="itemId">Item identifier.</param> public BaseItemScript AddItem(int itemId, int instanceId, int posX, int posY, bool immediate, bool ownedItem) { BaseItemScript builder = null; Debug.LogError("CreateItemID:" + itemId); if (!immediate) { // builder = this.GetFreeBuilder(); // if (builder == null) // { // Debug.Log("All builders are busy!"); // UIManager.instance.ShowBuildersBusyWindow(); // return null; // } } BaseItemScript instance = Utilities.CreateInstance(this.BaseItem, this.ItemsContainer, true).GetComponent <BaseItemScript>(); if (instanceId == -1) { instanceId = this._GetUnusedInstanceId(); } instance.instanceId = instanceId; this._itemInstances.Add(instanceId, instance); instance.SetItemData(itemId, posX, posY); instance.SetState(Common.State.IDLE); // GroundManager.Cell freeCell = GroundManager.instance.GetRandomFreeCellForItem (instance); // instance.SetPosition (GroundManager.instance.CellToPosition (freeCell)); if (!immediate) { instance.UI.ShowProgressUI(true); if (!instance.itemData.configuration.isCharacter && instance.itemData.configuration.buildTime > 0) { if (builder != null) { builder.BuilderAction(instance); } } } if (!instance.itemData.configuration.isCharacter) { GroundManager.instance.UpdateBaseItemNodes(instance, GroundManager.Action.ADD); } if (instance.itemData.name == "Wall") { this.UpdateWalls(); } instance.ownedItem = ownedItem; return(instance); }
public void UpdateBaseItemNodes(BaseItemScript item, Action action) { if (item != null) { Vector3 pos = item.GetPosition(); int x = (int)(pos.x); int z = (int)(pos.z); int sizeX = (int)item.GetSize().x; int sizeZ = (int)item.GetSize().z; for (int indexX = x; indexX < x + sizeX; indexX++) { for (int indexZ = z; indexZ < z + sizeZ; indexZ++) { bool isCellWalkable = false; if ((sizeX > 2 && indexX == x) || (sizeX > 2 && indexX == x + sizeX - 1) || (sizeZ > 2 && indexZ == z) || (sizeZ > 2 && indexZ == z + sizeZ - 1)) { //use this for make outer edge walkable for items have size morethan 2x2 isCellWalkable = true; } if (item.itemData.name == "ArmyCamp") { //make every cell walkable in army camp isCellWalkable = true; } if (action == Action.ADD) { //adding scene item to nodes, so walkable is false this.instanceNodes[indexX, indexZ] = item.instanceId; if (item.itemData.name == "Wall") { this.pathNodesWithoutWall[indexX, indexZ] = true; this.pathNodesWithWall[indexX, indexZ] = false; } else { this.pathNodesWithoutWall[indexX, indexZ] = isCellWalkable; } } else if (action == Action.REMOVE) { if (this.instanceNodes[indexX, indexZ] == item.instanceId) { this.instanceNodes[indexX, indexZ] = -1; this.pathNodesWithoutWall[indexX, indexZ] = true; this.pathNodesWithWall[indexX, indexZ] = true; } } } } } }
public void OnUnitDied(BaseItemScript unit) { _diedUnitCount++; if (_diedUnitCount == (_swordManCount + _archerCount)) { //war ends AttackOverlayWindowScript.instance.Close(); UIManager.instance.ShowResultWindow(false, _swordManExpended, _archerExpended); } }
public void RemoveItem(BaseItemScript item) { this._gameData.sceneData.RemoveItem(item.instanceId); this.SaveDataBase(); if (HonjinManager.instance.dicSDModel.ContainsKey(item.itemData.id)) { HonjinManager.instance.dicSDModel[item.itemData.id].SetTargetPos(HonjinManager.instance.dicSDModel[item.itemData.id].initalPos, 0); HonjinManager.instance.dicSDModel[item.itemData.id].st = Character.State.B_walk; } }
public void UpdateConnectedItems() { //add archers if it is a tower item if (this.itemData.name == "Tower") { BaseItemScript towerArcher = null; if (this.connectedItems.Count > 0) { towerArcher = this.connectedItems[0]; } if (towerArcher == null) { towerArcher = SceneManager.instance.AddItem(1502, true, ownedItem); this.connectedItems.Add(towerArcher); } towerArcher.SetPosition(this.GetCenterPosition() + new Vector3(0, 1.2f, 0)); towerArcher.SetState(state); towerArcher.SetDirection(direction); } //add builder to builder hut if (this.itemData.name == "BuilderHut" && SceneManager.instance.gameMode == Common.GameMode.NORMAL) { if (SceneManager.instance.selectedItem == this) { //that means the hut is on drag //builder comes to hut only after on stop drag return; } BaseItemScript builder = null; if (this.connectedItems.Count > 0) { builder = this.connectedItems[0]; } if (builder == null) { builder = SceneManager.instance.AddItem(3823, true, ownedItem); builder.SetPosition(this.GetRandomFrontCellPosition()); //connect builder item to the builder hut this.connectedItems.Add(builder); //connect this builder hut item to builder builder.connectedItems.Add(this); } builder.ReturnBuilder(); } }
void Start() { this._baseItem = this.GetComponentInParent <BaseItemScript>(); this._fillerFullLength = this.ProgressFiller.size.x; Vector3 baseSize = this._baseItem.GetSize(); this.ProgressContainer.localScale = this.ProgressContainer.localScale / baseSize.x; this.Init(); }
public void UpdateWalls() { foreach (KeyValuePair <int, BaseItemScript> entry in _itemInstances) { BaseItemScript item = entry.Value; if (item.itemData.name == "Wall") { item.UpdateWall(); } } }
void Awake() { this._baseItem = this.GetComponentInParent <BaseItemScript>(); if (this._baseItem == null) { return; } Vector3 baseSize = this._baseItem.GetSize(); this.Container.localScale = this.Container.localScale / baseSize.x; }
public void BuilderLoop() { if (Time.time - this._buildStartTime <= this.buildingItem.itemData.configuration.buildTime) { this.StartCoroutine(_BuilderLoop()); } else { this.buildingItem = null; this.Walker.OnFinishWalk -= this.BuilderLoop; this.ReturnBuilder(); } }
public void AttackNearestTarget() { BaseItemScript target = _GetNearestTargetItem(); if (target != null) { this.Attack(target); } else { int t = 0; } }
void Awake() { this._baseItem = this.GetComponentInParent <BaseItemScript>(); if (this._baseItem == null) { return; } this._fillerFullLength = this.ProgressFiller.size.x; Vector3 baseSize = this._baseItem.GetSize(); this.ProgressContainer.localScale = this.ProgressContainer.localScale / baseSize.x; }
public void LoadEnemyScene() { UIManager.instance.ShowAttackOverlayWindow(); this.ClearScene(); SceneData sceneData = DataBaseManager.instance.GetEnemyScene(); foreach (ItemData itemData in sceneData.items) { BaseItemScript baseItem = this.AddItem(itemData.itemId, itemData.instanceId, itemData.posX, itemData.posY, true, false); baseItem.OnItemDestroy += this.OnEnemyItemDestroy; } GroundManager.instance.UpdateAllNodes(); this.UpdateWalls(); }
/// <summary> /// Raises the item drag start event. /// </summary> /// <param name="evt">Evt.</param> public void OnItemDragStart(CameraManager.CameraEvent evt) { if (this.gameMode == Common.GameMode.ATTACK) { return; } this._dragItem = evt.baseItem; this._dragItem.OnItemDragStart(evt); if (this._dragItem.itemData.name == "Wall") { this.UpdateWalls(); } }
public void BuilderAction(BaseItemScript baseItem) { if (!this.itemData.configuration.isCharacter) { return; } this.buildingItem = baseItem; this._buildStartTime = Time.time; Debug.Log(this.buildingItem.GetPosition()); this.Walker.WalkToPosition(this.buildingItem.GetRandomFrontCellPosition()); this.Walker.OnFinishWalk += this.BuilderLoop; }
public BaseItemScript GetItemInPosition(Vector3 position) { int posX = (int)position.x; int posZ = (int)position.z; if (posX < 0 || posX >= nodeWidth || posZ < 0 || posZ >= nodeHeight) { return(null); } int itemInstanceId = GroundManager.instance.instanceNodes [posX, posZ]; BaseItemScript item = null; SceneManager.instance.GetItemInstances().TryGetValue(itemInstanceId, out item); return(item); }
public void Defend() { BaseItemScript target = _GetNearestTargetItem(); if (target != null) { this._currentTarget = target; this._currentTargetPoint = target.GetPosition(); this.DefendLoop(); } else { this.SearchForAttacker(); } }
public BaseItemScript GetNearestWallOnPath() { if (this._path != null && this._path.nodes != null && this._path.nodes.Length > 0) { for (int index = 0; index < this._path.nodes.Length; index++) { BaseItemScript item = GroundManager.instance.GetItemInPosition(this._path.nodes[index]); if (item != null && !item.isDestroyed && item.itemData.name == "Wall") { return(item); } } } return(null); }
public void Attack(BaseItemScript target) { this._baseItem.Walker.OnBetweenWalk = null; if (target != null) { this._currentTarget = target; Vector3[] outerCells = target.GetOuterCells(); if (outerCells.Length > 0) { //finding nearest outer cell Vector3 nearestCell = Vector3.one * 999999; float smallerDistance = 999999; for (int index = 0; index < outerCells.Length; index++) { float distance = Vector3.Distance(this._baseItem.GetPosition(), outerCells[index]); if (distance < smallerDistance) { smallerDistance = distance; nearestCell = outerCells[index]; } } this._currentTargetPoint = nearestCell; this._baseItem.Walker.WalkToPosition(_currentTargetPoint); this._baseItem.Walker.OnFinishWalk += this.AttackLoop; //check any walls on root BaseItemScript nearestWallOnPath = this._baseItem.Walker.GetNearestWallOnPath(); if (nearestWallOnPath != null) { this._currentTarget = nearestWallOnPath; // Debug.Log (nearestWallOnPath); } } if (this._baseItem.itemData.configuration.attackRange > 0) { this._baseItem.Walker.OnBetweenWalk += this.CheckTargetBeyondRange; //check on start walk because the unit will already in range of target maybe this.CheckTargetBeyondRange(); } } }
public void UpdateBaseItemTap() { if (!Input.GetMouseButtonUp(0)) { return; } if (this._isPanningSceneStarted) { return; } if (this._isDraggingBaseItem) { return; } if (this.IsUsingUI()) { return; } BaseItemScript baseItemTapped = this._TryGetRaycastHitBaseItem(Input.mousePosition); if (baseItemTapped != null) { this._isTappedBaseItem = true; this._selectedBaseItem = baseItemTapped; CameraEvent evt = new CameraEvent() { baseItem = baseItemTapped }; if (this.OnItemTap != null) { this.OnItemTap.Invoke(evt); } } else { this._isTappedBaseItem = false; this._selectedBaseItem = null; } }
//private int _barrackID = 8833; //private int _elixirCollectorID = 4856; //private int _elixirStorageID = 2090; //private int _goldMineID = 3265; //private int _goldStorageID = 9074; //private int _towerID = 4764; //private int _townCenterID = 2496; //private int _windMillID = 6677; //private int _armyCampID = 2728; //private BaseItemScript _townCenterInstance; //private BaseItemScript _builderHutInstance; //private BaseItemScript _builderInstance; //private BaseItemScript _towerInstance; //private BaseItemScript _armyCampForSwordManInstance; //private BaseItemScript _armyCampForArcherInstance; //public BaseItemScript testCharacter; //private BaseItemScript[] _swordManInstances; //private BaseItemScript[] _archerInstances; public void LoadUserScene() { this.ClearScene(); SceneData sceneData = DataBaseManager.instance.GetScene(); foreach (ItemData itemData in sceneData.items) { this.AddItem(itemData.itemId, itemData.instanceId, itemData.posX, itemData.posZ, true, true); } //LOAD UNITS ON CAMP BaseItemScript[] armyCamps = GetArmyCamps(); if (armyCamps.Length > 0) { for (int index = 0; index < _swordManCount; index++) { var camp = armyCamps[Random.Range(0, armyCamps.Length)]; BaseItemScript unit = this.AddItem(_swordMan_ID, -1, camp.GetPositionX(), camp.GetPositionZ(), true, true); unit.WalkRandom(camp); } for (int index = 0; index < _archerCount; index++) { var camp = armyCamps[Random.Range(0, armyCamps.Length)]; BaseItemScript unit = this.AddItem(_archer_ID, -1, camp.GetPositionX(), camp.GetPositionZ(), true, true); unit.WalkRandom(camp); } } //for (int index = 0; index < 25; index++) //{ // //tree // BaseItemScript tree = this.AddItem(5341, true, true); // tree.SetPosition(GroundManager.instance.GetRandomFreePosition()); //} GroundManager.instance.UpdateAllNodes(); this.UpdateWalls(); UIManager.instance.ShowGameOverlayWindow(); }