public static void DistributeIDComponentUpdate(IDComponent sender,IDComponentUpdateMsg msg) { msg.id = sender.getID(); msg.componentName = sender.GetType ().ToString (); if (instance.mode) NetworkServer.SendToAll (MyMsgType.IDComponentUpdateMessage, msg); else { client.Send(MyMsgType.IDComponentUpdateMessage,msg); } }
protected override void OnUpdate() { float deltaTime = Time.DeltaTime; EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager; Entities.ForEach((Entity unitEntity, ref HasTarget _hasTarget, ref Translation translation, ref IDComponent _id, ref MovementComponent _move) => { if (entityManager.Exists(_hasTarget.targetEntity)) { Translation targetTranslation = entityManager.GetComponentData <Translation>(_hasTarget.targetEntity); IDComponent targetID = entityManager.GetComponentData <IDComponent>(_hasTarget.targetEntity); if (math.distancesq(translation.Value, targetTranslation.Value) < .1f) { // Close to target, destroy it //Debug.Log("damage target id: " + targetID.id); //GameController.Instance.DamageUnit(targetID.id, 20, 0); //PostUpdateCommands.DestroyEntity(hasTarget.targetEntity); //PostUpdateCommands.RemoveComponent(unitEntity, typeof(HasTarget)); } else if (_move.chaseTarget == true) { moveTimer -= UnityEngine.Time.deltaTime; if (moveTimer < 0) { _move.isMoving = true; PathfindingGridSetup.Instance.pathfindingGrid.GetXY(targetTranslation.Value, out int endX, out int endY); // + new Vector3(1,1,0)* cellSize ValidateGridPosition(ref endX, ref endY); if (PathfindingGridSetup.Instance.pathfindingGrid.GetGridObject(endX, endY).IsWalkable()) { PathfindingGridSetup.Instance.pathfindingGrid.GetXY(translation.Value, out int startX, out int startY); ValidateGridPosition(ref startX, ref startY); EntityManager.AddComponentData(unitEntity, new DestinationComponent { startPosition = new int2(startX, startY), endPosition = new int2(endX, endY) }); } else { Debug.Log("target is not reachable!!"); } moveTimer = 2; } } } else { // Target Entity already destroyed PostUpdateCommands.RemoveComponent(unitEntity, typeof(HasTarget)); Debug.Log("remove Entity hastarget"); } }); }
protected override void OnUpdate() { checkStateTimer -= UnityEngine.Time.deltaTime; if (checkStateTimer <= 0) { Entities.ForEach((Entity entity, ref OrderComponent _orders, ref StateComponent _state, ref HealthComponent _health, ref IDComponent _id, ref DeathComponent _death) => { if (!_orders.hasOrders) { int myID = _id.id; int myState = 3; // CHECK IF UNDER ATTACK Entities.ForEach((Entity _entity, ref HasTarget _target) => { IDComponent targetID = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData <IDComponent>(_target.targetEntity); if (myID == targetID.id) { myState = 1; } }); if (myState == 1) { _state.state = 1; Debug.Log("Under attack!"); } if (_state.state == 0) { //Debug.Log(_id.id + " is idle"); _state.state = 3; PostUpdateCommands.AddComponent(entity, new RoamingComponent { }); } if (_state.state == 1) { //Debug.Log(_id.id + " is in combat"); Entities.ForEach((Entity moraleEntity, ref StateComponent _moraleState, ref HealthComponent _moraleHealth, ref IDComponent _moraleId, ref MoraleComponent _morale) => { if (_morale.healthModifier < 1) { _moraleState.state = 4; //Debug.Log(_moraleId.id + " is panicking!!"); } if (_moraleHealth.health / _moraleHealth.maxHealth <= 0.8) { _morale.healthModifier = -1; } else if (_moraleHealth.health / _moraleHealth.maxHealth <= 0.5) { _morale.healthModifier = -2; } else { _morale.healthModifier = -4; } }); } if (_state.state == 2) { //Debug.Log(_id.id + " is doing task"); } if (_state.state == 3) { //Debug.Log(_id.id + " is roaming"); } if (_state.state == 4) { Debug.Log(_id.id + " is Fleeing"); } } else { _state.state = 2; } }); checkStateTimer = 1; } }
protected override void OnUpdate() { Entities.ForEach((Entity entity, ref IDComponent _outerID, ref Translation _translation, ref HasTarget _hasTarget, ref AttackComponent _attack, ref WeaponComponent _weapon, ref StateComponent _state) => { ComponentDataFromEntity <Translation> allTranslations = GetComponentDataFromEntity <Translation>(true); ComponentDataFromEntity <DeathComponent> allDead = GetComponentDataFromEntity <DeathComponent>(true); DeathComponent dead = allDead[_hasTarget.targetEntity]; if (_hasTarget.targetEntity != Entity.Null || !dead.isDead) { Translation targetTranslation = allTranslations[_hasTarget.targetEntity]; if (World.DefaultGameObjectInjectionWorld.EntityManager.Exists(_hasTarget.targetEntity) && _attack.range >= math.distance(_translation.Value, targetTranslation.Value) && _attack.isAttacking == true) { _attack.timer -= UnityEngine.Time.deltaTime; if (_attack.timer < 0) { IDComponent attackerID = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData <IDComponent>(entity); if (isDebug) { Debug.Log("Attack!"); } IDComponent targetID = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData <IDComponent>(_hasTarget.targetEntity); //GameController.Instance.DamageUnit(attackerID.id, targetID.id, _weapon.damage, GameController.damageType.Physical); _attack.timer = 10 / _attack.nrOfAttacks; float dmg = _weapon.damage; int outerID = _outerID.id; int newState = 1; Entities.ForEach((Entity innerEntity, ref IDComponent _innerID, ref HealthComponent _health, ref DeathComponent _death) => { if (targetID.id == _innerID.id) { if (isDebug) { Debug.Log(outerID + " hits " + _innerID.id + " with " + dmg + " damage"); } _health.health -= dmg; if (isDebug) { Debug.Log(_innerID.id + " has " + _health.health + " health left"); } if (_health.health <= 0) { _death.isDead = true; dead.isDead = true; newState = 0; if (isDebug) { Debug.Log(_innerID.id + " has died in combat"); } } PostUpdateCommands.AddComponent(innerEntity, new DoNotTarget { }); } }); if (newState == 0) { _state.state = 0; } if (dead.isDead) { if (isDebug) { Debug.Log("combat removing hastarget"); } Debug.Log("combat going idle"); PostUpdateCommands.AddComponent(entity, new IdleComponent { }); PostUpdateCommands.RemoveComponent(entity, typeof(HasTarget)); } } } } else { _state.state = 0; Debug.Log(_outerID + " has no target going idle"); } }); }