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() { 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); }
public override void OnTick() { if (collector != null && !collector.entity.isAlive) { collector = null; } if (resource != null && !resource.entity.isAlive) { resource = null; } if (resource != null && resource.occupied) { resource = null; } if (is_idle) { return; } if (moving_to_collector) { if (collector == null) { // Find the nearest collector. var ent = World.current.entities .Where(e => e.team == entity.team && e.GetComponents <ResourceCollectionPoint>().Any(s => s.resourceId == resourceId)) .OrderBy(e => World.current.map.Distance(entity.position, e.position)) .FirstOrDefault(); if (ent != null) { collector = ent.GetComponents <ResourceCollectionPoint>().Where(s => s.resourceId == resourceId).FirstOrDefault(); } } if (collector == null) { motor.Stop(); is_idle = true; return; } var dist = World.current.map.Distance(new DVector3(entity.position.x, 0, entity.position.z), new DVector3(collector.entity.position.x, 0, collector.entity.position.z)); if (dist < 3) { motor.Stop(); // At the dropoff, disgorge the tank contents. var transfered = DReal.Min(fill, fillRate * World.deltaTime); collector.Receive(transfered); fill -= transfered; } else { motor.MoveTo(collector.entity.position); } // If the tank is empty, move to the resource. if (fill <= 0) { moving_to_collector = false; motor.Stop(); } } else { if (resource != null && resource.remainingCount <= 0) { resource = null; } if (resource == null) { // Find the nearest resource. var ent = World.current.entities .Where(e => e.GetComponents <ResourceSource>().Any(s => s.resourceId == resourceId && s.remainingCount > 0 && !s.occupied)) .OrderBy(e => World.current.map.Distance(entity.position, e.position)) .FirstOrDefault(); if (ent != null) { resource = ent.GetComponents <ResourceSource>().Where(s => s.resourceId == resourceId && s.remainingCount > 0 && !s.occupied).FirstOrDefault(); } } if (resource == null) { motor.Stop(); moving_to_collector = true; return; } var dist = World.current.map.Distance(new DVector3(entity.position.x, 0, entity.position.z), new DVector3(resource.entity.position.x, 0, resource.entity.position.z)); if (dist < 3) { motor.Stop(); // At the pickup, fill the tank. var transfered = DReal.Min(capacity - fill, fillRate * World.deltaTime); fill += resource.Take(transfered); } else { motor.MoveTo(resource.entity.position); } // If the tank is full, move to the collector. if (fill >= capacity) { moving_to_collector = true; motor.Stop(); } } }