virtual public void UpdateState(NewPackets.EntityUpdate newState) { // save original state var oldType = Type; var oldTilePos = tilePos; var oldHidden = Hidden; var oldHealthPoints = HealthPoints; var oldHealthCapacity = HealthCapacity; var oldCarried = Carried; var oldCarryCapacity = CarryCapacity; var oldStatus = Status; var oldAttacking = Attacking; var oldTarget = AttackTarget; // Copy state Type = newState.unitType; tilePos = new Vector2Int(newState.position.X, newState.position.Y); Hidden = newState.hidden; HealthPoints = newState.health; HealthCapacity = newState.healthCapacity; Carried = (byte)newState.inventory.Count; CarryCapacity = newState.carryCapacity; Inventory = newState.inventory; // TODO: add oldInventory? Status = newState.status; AttackTarget = NeoPlayer.Instance.MapManager.WorldPosFromGridIndex(new Vector2Int(newState.attackTarget.X, newState.attackTarget.Y)); Attacking = newState.attacking; AttackRange = newState.attackRange; MovementSpeed = (float)newState.moveSpeed; if (Attacking) { Debug.DrawLine(transform.position, AttackTarget, Color.red); } // called on every update OnUpdate(); // Die first if (newState.dead) { OnDeath(); } if (oldHealthPoints > HealthPoints) { OnDamage(); } if (oldHealthPoints != HealthPoints) { OnHealthChange(); } if (oldTarget != AttackTarget || oldAttacking != Attacking) { OnTargetChange(); } // called if a move occured if (oldTilePos != tilePos) { OnMove(tilePos); } if (oldStatus != EntityStatus.Idle && Status == EntityStatus.Idle) { OnIdle(); } if (Status == EntityStatus.Idle) { IsIdle(); } // TODO: talk about making carried an array if (Carried >= CarryCapacity && oldCarried != Carried) { OnFull(); } // Hide carried or destroyed entities if (Hidden) { OnHidden(); } if (NeoPlayer.Instance.myFactionID != factionID) { if (_otherDecay != null) { StopCoroutine(_otherDecay); } _otherDecay = StartCoroutine(Decay()); } UpdateListener(); }
public void RecieveEntityUpdate(byte[] packet) { // Get the faction index from the header byte factionIndex = 0; packet = NewPackets.PopByteHeader(packet, out factionIndex); // Deserialize NewPackets.EntityUpdate entityUpdate = new NewPackets.EntityUpdate(packet); if (FactionEntities[factionIndex, entityUpdate.unitIndex] == null) { // Implicitly populate faction entities switch (entityUpdate.unitType) { // Units case Constants.Entities. Miner.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(factionIndex == 1 ? Faction1Miner : Faction2Miner).GetComponent <Entity>(); break; case Constants.Entities. Digger.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(factionIndex == 1 ? Faction1Digger : Faction2Digger).GetComponent <Entity>(); break; case Constants.Entities. Soldier.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(factionIndex == 1 ? Faction1Soldier : Faction2Soldier).GetComponent <Entity>(); break; case Constants.Entities. Hauler.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(factionIndex == 1 ? Faction1Hauler : Faction2Hauler).GetComponent <Entity>(); break; //... // Deployables case Constants.Entities. QuickSand.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(CementBombEntityPrefab).GetComponent <Entity>(); break; case Constants.Entities. TimeBomb.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(TimeBombEntityPrefab).GetComponent <Entity>(); break; case Constants.Entities. ProximityMine.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(MineEntityPrefab).GetComponent <Entity>(); break; case Constants.Entities. GasBomb.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(GasBombEntityPrefab).GetComponent <Entity>(); break; case Constants.Entities. HealStation.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(HealStationEntityPrefab).GetComponent <Entity>(); break; case Constants.Entities. SonicBomb.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(SonicBombEntityPrefab).GetComponent <Entity>(); break; case Constants.Entities. AntimatterBomb.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(AntimatterBombEntityPrefab).GetComponent <Entity>(); break; case Constants.Entities. SandBags.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(SandBagsEntityPrefab).GetComponent <Entity>(); break; // Hub case Constants.Entities.Hub.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(factionIndex == 1 ? Faction1Hub : Faction2Hub).GetComponent <Entity>(); break; // Ore case Constants.Entities.Ore.ID: FactionEntities[factionIndex, entityUpdate.unitIndex] = GameObject.Instantiate(OreEntityPrefab).GetComponent <Entity>(); break; default: Debug.Log("PLAYER ERROR: NO CASE FOR THAT ENTITY!"); break; } // Manually set indices for requests FactionEntities[factionIndex, entityUpdate.unitIndex].factionID = factionIndex; FactionEntities[factionIndex, entityUpdate.unitIndex].entityID = entityUpdate.unitIndex; } // Route new state to correct unit int correct faction FactionEntities[factionIndex, entityUpdate.unitIndex].UpdateState(entityUpdate); //print("GOT ENTITY UPDATE"); }
override public void UpdateState(NewPackets.EntityUpdate newState) { base.UpdateState(newState); }