public void Tick() { currentTick += 1; // Call OnCreate on each new entity. while (new_entities.Count != 0) { var e = new_entities[new_entities.Count - 1]; new_entities.RemoveAt(new_entities.Count - 1); e.OnCreate(); _entities.Add(e); eventListener.EntityCreated(e); } // OnTick. foreach (var e in entities) { e.OnTick(); } // Perform collision detection. for (var i = 0; i < colliders.Count; i += 1) { var ci = colliders[i]; var i_bot = ci.entity.position.y; var i_top = i_bot + ci.height; for (var j = i + 1; j < colliders.Count; j += 1) { var cj = colliders[j]; var j_bot = cj.entity.position.y; var j_top = j_bot + cj.height; var size = ci.radius + cj.radius; var dist_sqr = map.DistanceSqr(ci.entity.position, cj.entity.position); if (dist_sqr > size * size) { continue; } if (DReal.Max(i_bot, j_bot) > DReal.Min(i_top, j_top)) { continue; } ci.entity.OnCollision(cj.entity); cj.entity.OnCollision(ci.entity); } } // Call OnDestroy on each dead entity. while (dead_entities.Count != 0) { var e = dead_entities[dead_entities.Count - 1]; dead_entities.RemoveAt(dead_entities.Count - 1); if (new_entities.Contains(e)) { new_entities.Remove(e); } else { e.OnDestroy(); _entities.Remove(e); eidToEntityMap.Remove(e.eid); eventListener.EntityDestroyed(e); } } }
public override void OnTick() { if (health != null) { var missing_health = health.max - health.current; health.max = DReal.Max(1, realMaxHealth * buildProgress); health.current = health.max - missing_health; } }
public override void OnCreate() { motor = entity.GetComponent <IMotor>(); weapons = entity.GetComponents <IWeapon>().ToArray(); weaponRange = weapons.Aggregate((DReal)0, (x, y) => DReal.Max(x, y.Range())); Logger.Log("Weapon range is {0}", weaponRange); if (weapons.Length == 0) { stance = Stance.Passive; } else { stance = Stance.Active; } }
// Range to target, adjusted by collider size. public DReal Range(Entity target) { var ent2d = new DVector3(position.x, 0, position.z); var targ2d = new DVector3(target.position.x, 0, target.position.z); var dist = World.current.map.Distance(ent2d, targ2d); var local_collider = GetComponent <Collider>(); if (local_collider != null) { dist -= local_collider.radius; } var target_collider = target.GetComponent <Collider>(); if (target_collider != null) { dist -= target_collider.radius; } return(DReal.Max(0, dist)); }
public override void OnTick() { var p2d = new DVector3(entity.position.x, 0, entity.position.z); var targ = new Game.DVector3(target.x, 0, target.z); var dist = World.current.map.Distance(p2d, targ); var dir = World.current.map.Direction(p2d, targ); if (dist < moveSpeed * World.deltaTime) { entity.position = new DVector3(target.x, entity.position.y, target.z); } else { var d = p2d + dir * moveSpeed * World.deltaTime; entity.position = new DVector3(d.x, entity.position.y, d.z); entity.faceDirection = dir; } var terrainHeight = DReal.Max(0, World.current.map.Height(entity.position)); var wobble = DReal.Sin((World.current.time + wobbleOffset) * wobbleFrequency) * wobbleAmplitude; var targetHeight = terrainHeight + floatOffset + wobble; var height_diff = DReal.Abs(targetHeight - entity.position.y); var newHeight = (DReal)0; if (targetHeight > entity.position.y) { newHeight = entity.position.y + DReal.Min(height_diff, verticalSpeed * World.deltaTime); } else { newHeight = entity.position.y - DReal.Min(height_diff, verticalSpeed * World.deltaTime); } entity.position = new DVector3(entity.position.x, DReal.Max(terrainHeight, newHeight), entity.position.z); }