protected override void moveTowardsObject(GameObject obj) { acc.X = 0; acc.Y = 0; if (obj.isAlive) { float diffX = pos.X - obj.pos.X; float diffY = pos.Y - obj.pos.Y; if (Math.Abs(diffX) < SightRange && Math.Abs(diffY) < SightRange) { float factor = (float)Math.Sqrt(diffX * diffX + diffY * diffY); acc.X = -Acceleration * diffX / factor; acc.Y = -Acceleration * diffY / factor; } } else { float diffX = pos.X - master.pos.X; float diffY = pos.Y - master.pos.Y; float factor = (float)Math.Sqrt(diffX * diffX + diffY * diffY); acc.X = -Acceleration * diffX / factor; acc.Y = -Acceleration * diffY / factor; } }
// Check collision with a specific object and update the collision state private CollisionState getCollisionState(GameObject obj, float timeDelta) { // Quick check if (Methods.absDiff(pos.X, obj.pos.X) > radius + obj.radius) return CollisionState.None; if (Methods.absDiff(pos.Y, obj.pos.Y) > radius + obj.radius) return CollisionState.None; // Calculate exact collision distance float collisionDistance = getCollisionDistanceSquared(obj); if (collisionDistance <= 0) { return CollisionState.Touching; } else { return CollisionState.None; } }
// Calculate the squared distance from a specific object private float getCollisionDistanceSquared(GameObject obj) { float sumRadiusSquared = (this.radius + obj.radius) * (this.radius + obj.radius); float collisionDistance = Methods.lengthSquared3(this.pos, obj.pos) - sumRadiusSquared; return collisionDistance; }
// Attack a specific object, amount is the percentage Damage dealt (1 = 100%) private void Attack(GameObject obj, float amount) { // Update attack delay and hit delay counters // - If attack speed or damage is less than or equal to 0, then don't attack at all if (AttackSpeed <= 0 || Damage <= 0) return; else attackCounter = AttackSpeed; // Objects outside of the screen are invulnerable if (!obj.isInBoundaries()) return; // game.soundController.PlaySound("hit.wav", 0.6); float totalDamage = Damage * amount; totalDamage += ((float)game.random.NextDouble() - 0.5f) * totalDamage * 0.4f; if (obj.HitPoints - totalDamage <= 0) // Remove victim if its HitPoints reaches 0 { obj.HitPoints = 0; obj.Kill(); } else { obj.HitPoints -= totalDamage; // Remove HP from victim } }
// What to do during collision event public virtual void Hit(float timeDelta, GameObject obj) { }
private void seekNearestTarget() { acc.X = 0; acc.Y = 0; float diffX = 10000; float diffY = 10000; if (target == null) { foreach (var obj in game.gameObjects) { if (Type == GameObjectType.ProjectilePlayer) { if (obj.Type == GameObjectType.Enemy || obj.Type == GameObjectType.Boss) { float x = pos.X - obj.pos.X; float y = pos.Y - obj.pos.Y; if (Math.Abs(diffX) > Math.Abs(x)) { diffX = x; target = obj; } if (Math.Abs(diffY) > Math.Abs(y)) { diffY = y; target = obj; } } } else { if (obj.Type == GameObjectType.Player) { float x = pos.X - obj.pos.X; float y = pos.Y - obj.pos.Y; if (Math.Abs(diffX) > Math.Abs(x)) { diffX = x; target = obj; } if (Math.Abs(diffY) > Math.Abs(y)) { diffY = y; target = obj; } } } } } else { if (!target.isAlive) { target = null; return; } diffX = pos.X - target.pos.X; diffY = pos.Y - target.pos.Y; } float factor = (float)Math.Sqrt(diffX * diffX + diffY * diffY); acc.X = -Acceleration * diffX / factor; acc.Y = -Acceleration * diffY / factor; }
// Follow target object, allows camera rotation about X and Y axis (no rolling) public void OrbitTarget(GameObject obj, Vector3 distance) { if (obj != null) { cameraMode = CameraMode.orbit; targetObject = obj; offsetDistance = distance; } else { FreeMode(); } }
public void FreeMode() { cameraMode = CameraMode.free; targetObject = null; offsetDistance = Vector3.Zero; }
// Follow target object at a height directly above target public void BirdsEyeView(GameObject obj, float height) { ChaseTarget(obj, new Vector3(0, 0, height)); }
public override void Hit(float timeDelta, GameObject obj) { vel.X *= -1 * Constants.ElasticModulus; vel.Y *= -1 * Constants.ElasticModulus; base.Hit(timeDelta, obj); }
protected virtual void moveTowardsObject(GameObject obj) { if (obj == null) return; // Can't follow an object that doesn't exist //acc.X = 0; //acc.Y = 0; if (obj.isAlive) { float diffX = pos.X - obj.pos.X; float diffY = pos.Y - obj.pos.Y; if (diffX == 0 && diffY == 0) return; // Already at object's position //if (Math.Abs(diffX) < SightRange && Math.Abs(diffY) < SightRange) //{ float factor = (float)Math.Sqrt(diffX * diffX + diffY * diffY); acc.X = -Acceleration * diffX / factor; acc.Y = -Acceleration * diffY / factor; //} } }