public BulletModel(string visual, Vector2 position, UnitModel target, float speed) { Visual = visual; _startPosition = position; _target = target; _speed = speed; Position = position + (_target.Position - position).normalized / 2f; _startPosition = Position; Direction = (_target.Position - Position).normalized; _time = Position.DistanceTo(_target.Position) / speed; }
public void Update(float deltaTime) { if (_target != null) { if (Position.DistanceTo(_target.Position) > TowerAI.Radius) { ClearTarget(); } } if (_target == null) { var left = Position.x - TowerAI.Radius; var right = Position.x + TowerAI.Radius; var top = Position.y + TowerAI.Radius; var bottom = Position.y - TowerAI.Radius; float distance = TowerAI.Radius; UnitModel selectedUnit = null; foreach (var unit in _map.Units) { if (unit.Position.x >= left && unit.Position.y <= right && unit.Position.y >= bottom && unit.Position.y <= top) { var dist = Position.DistanceTo(unit.Position); if (dist <= distance) { distance = dist; selectedUnit = unit; } } } if (selectedUnit != null) { SetTarget(selectedUnit); } } Direction = _target == null ? Vector2.up : (_target.Position - Position).normalized; if (_target != null) { TowerAI.Update(deltaTime); } }
public RocketModel(string visual, Vector2 position, UnitModel target, float speed) : base(visual, position, target, speed) { _startDistance = Position.DistanceTo(_target.Position); _time = Mathf.Sqrt(2 * _startDistance / speed); }