private bool handleUnitCommand(Entity entity, ActionCommand command) { // Check if the entity is a Unit. Reject the command if the entity is not a Unit. if (entity.getEntityType() != Entity.EntityType.Unit) { return false; } // Get the action queue from the entity and clear it. Queue<ActionCommand> queue = entity.getActionQueue(); queue.Clear(); // Give the entity the command. entity.getActionQueue().Enqueue(command); return true; }
/// <summary> /// This method will remove a Entity from the game. /// </summary> /// <param name="entity">The Entity being removed.</param> public void removeEntity(Entity entity) { if (entity.getEntityType() == Entity.EntityType.Unit) // entity is a Unit { // Remove Unit from its cell Unit u = (Unit)entity; Cell c = u.getCell(); c.removeUnit(); u.setCell(null); // Remove Unit from the Unit List. gw.getUnits().Remove(u); } else { gw.remove((StaticEntity)entity); } // Remove entity from its player. scenario.removeEntityFromPlayer(entity); }
/// <summary> /// The basic outline of the updateEntity method is as follows: /// 1, Have the entity perform the current action at the top of it's action queue (if any) /// 2, Check for changes in the entity's stats. (ex: check for death.) /// 3, Have the Entity react to any Events that occur within it's visibility range. /// (Events that either occur to the Entity or events that the Entity can "see") /// 4, Check if the Entity should be removed from the game. (When it's primary state is set to Remove.) /// </summary> /// <param name="entity"></param> /// <param name="entitiesToRemove"></param> private void updateEntity(Entity entity, List<Entity> entitiesToRemove) { /*** Have entity perform it's current action (if any) ***/ ActionController.Instance.update(entity, locController); Entity.EntityType type = entity.getEntityType(); /*** Update stats of Entity ***/ if (type == Entity.EntityType.Unit) { UnitStatsLogic.updateUnit((Unit)entity, curTick); } else { // Only Stats update occurs upon death of any StaticEntity right? if (entity.health <= 0) { entity.getState().setPrimaryState(State.PrimaryState.Dead); } } /*** Have Entity react to any Events ***/ if (type == Entity.EntityType.Unit) // Only Units really need to react to Events. { } /*** Remove Entity if it needs to be removed. ***/ if (entity.getState().getPrimaryState() == State.PrimaryState.Dead) // Should be remove. Changed for demo. { entitiesToRemove.Add(entity); } }
/// <summary> /// This method will attempt to add the entity at the given location. /// </summary> /// <param name="entity"></param> /// <param name="x">x coordinate in gamespace of where the entity should be inserted</param> /// <param name="y">y coordinate in gamespace of where the entity should be inserted</param> /// <returns>true if the entity is inserted successfully, false otherwise</returns> public bool addEntity(Entity entity, float x, float y) { bool success = false; if (x < 0 || x >= gw.map.width || y < 0 || y >= gw.map.height) { return false; } // The coordinates of the Cell the entity is being inserted into. int xC = (int)Math.Floor(x); int yC = (int)Math.Floor(y); Cell c = gw.map.getCell(xC, yC); /** Try Inserting into GameWorld first **/ if (entity.entityType == Entity.EntityType.Unit) { // Insert into gameworld if the target cell is empty if (c.isValid) { Unit u = (Unit)entity; c.setUnit(u); // Insert into the cell gw.getUnits().Add(u); // Insert into the Unit list u.setCell(c); u.x = (float)xC + 0.5f; u.y = (float)yC + 0.5f; success = true; visMapLogic.updateVisMap(u); } } else if(entity.getEntityType() == Entity.EntityType.Building) { Building b = (Building)entity; b.setOrginCell(c); b.health = 0; success = gw.insert(b, c); if (success) { ///Player pays for building costs scenario.getPlayer().player_resources[0] -= b.stats.waterCost; scenario.getPlayer().player_resources[1] -= b.stats.lumberCost; scenario.getPlayer().player_resources[2] -= b.stats.foodCost; scenario.getPlayer().player_resources[3] -= b.stats.metalCost; visMapLogic.updateVisMap(b); } } // If insert into GameWorld was a success, insert into right player if (success) { scenario.insertEntityIntoPlayer(entity); } return success; }
/// <summary> /// This method will attempt to add the entity at the given location. /// </summary> /// <param name="entity"></param> /// <param name="x">x coordinate in gamespace of where the entity should be inserted</param> /// <param name="y">y coordinate in gamespace of where the entity should be inserted</param> /// <returns>true if the entity is inserted successfully, false otherwise</returns> public bool addEntity(Entity entity, float x, float y) { bool success = false; if (x < 0 || x >= gw.map.width || y < 0 || y >= gw.map.height) { return false; } // The coordinates of the Cell the entity is being inserted into. int xC = (int)Math.Floor(x); int yC = (int)Math.Floor(y); Cell c = gw.map.getCell(xC, yC); /** Try Inserting into GameWorld first **/ if (entity.entityType == Entity.EntityType.Unit) { // Insert into gameworld if the target cell is empty if (c.isValid) { Unit u = (Unit)entity; c.setUnit(u); // Insert into the cell gw.getUnits().Add(u); // Insert into the Unit list u.setCell(c); u.x = (float)xC + 0.5f; u.y = (float)yC + 0.5f; success = true; visMapLogic.updateVisMap(u); // Update the Cells that the unit is observing. updateCellsUnitIsObserving(u); // Notify all observers of the cell that a unit has moved onto it. c.notify(new ZRTSModel.GameEvent.GameEvent(c, u, u, ZRTSModel.GameEvent.GameEvent.EventType.MoveEvent)); } } else if(entity.getEntityType() == Entity.EntityType.Building) { Building b = (Building)entity; b.setOrginCell(c); success = gw.insert(b, c); if (success) { ///Player pays for building costs scenario.getPlayer().player_resources[0] -= b.stats.waterCost; scenario.getPlayer().player_resources[1] -= b.stats.lumberCost; scenario.getPlayer().player_resources[2] -= b.stats.foodCost; scenario.getPlayer().player_resources[3] -= b.stats.metalCost; visMapLogic.updateVisMap(b); } } // If insert into GameWorld was a success, insert into right player if (success) { //System.Console.Out.WriteLine("Success fullying "); scenario.insertEntityIntoPlayer(entity); } return success; }
/// <summary> /// The basic outline of the updateEntity method is as follows: /// 1, Have the entity perform the current action at the top of it's action queue (if any) /// 2, Check for changes in the entity's stats. (ex: check for death.) /// 3, Have the Entity react to any Events that occur within it's visibility range. /// (Events that either occur to the Entity or events that the Entity can "see") /// 4, Check if the Entity should be removed from the game. (When it's primary state is set to Remove.) /// </summary> /// <param name="entity">Entity being updated.</param> /// <param name="entitiesToRemove">List of Entities to be removed.</param> private void updateEntity(Entity entity, List<Entity> entitiesToRemove) { /*** Have entity perform it's current action (if any) ***/ ActionController.Instance.update(entity, locController); Entity.EntityType type = entity.getEntityType(); // Enity's type (Unit, Building, Object or Resource) State state = entity.getState(); // Entity's State. /*** Update stats of Entity ***/ if (type == Entity.EntityType.Unit) { UnitStatsLogic.updateUnit((Unit)entity, curTick); } else { // Only Stats update occurs upon death of any StaticEntity right? if (entity.health <= 0) { state.setPrimaryState(State.PrimaryState.Dead); entity.tickKilled = curTick; } } /*** Have Entity react to any Events ***/ if (type == Entity.EntityType.Unit) // Only Units really need to react to Events. { GameEventLogic.processEvents((Unit)entity, scenario.getGameWorld()); } /*** Remove Entity if it needs to be removed. ***/ if (state.getPrimaryState() == State.PrimaryState.Dead) { entitiesToRemove.Add(entity); } }