public MovingObject(Vector2 startPos, Vector2 direction, int speed, float radius) { Position = startPos; Speed = speed; UnitDirectionVector = VMath.GetUnitVector(direction); Radius = radius; Health = 1; }
public Asteroid Half() { Radius /= 2; Speed *= 2; UnitDirectionVector = VMath.RotateVector(UnitDirectionVector, 25); Asteroid splitAsteroid = new Asteroid(Position, UnitDirectionVector, Speed, Radius); if (CanSplit()) { splitAsteroid.UnitDirectionVector = VMath.RotateVector(UnitDirectionVector, -50); } else { Destroy(); splitAsteroid.Health = 0; } return(splitAsteroid); }
public override void Draw(CanvasDrawingSession canvas) { if (LastFired < FIRE_COOLDOWN) { LastFired++; } Vector2 triangleTip = VMath.AddVectors(Position, VMath.ScalarMultiply(UnitDirectionVector, 12)); Vector2 triangleLeft = VMath.AddVectors( VMath.AddVectors(Position, VMath.ScalarMultiply(UnitDirectionVector, -12)) , VMath.ScalarMultiply(VMath.GetPerpendicularVector(UnitDirectionVector), -8)); Vector2 triangleRight = VMath.AddVectors( VMath.AddVectors(Position, VMath.ScalarMultiply(UnitDirectionVector, -12)) , VMath.ScalarMultiply(VMath.GetPerpendicularVector(UnitDirectionVector), 8)); Vector2[] trianglePoints = { triangleTip, triangleLeft, triangleRight }; CanvasGeometry triangle = CanvasGeometry.CreatePolygon(canvas, trianglePoints); canvas.FillGeometry(triangle, Colors.CadetBlue); }
public void Move(int canvasWidth, int canvasHeight) { Position = VMath.AddVectors(Position, VMath.ScalarMultiply(UnitDirectionVector, Speed)); if (!(this is Bullet)) { if (Position.X > canvasWidth) { Position = new Vector2(0, Position.Y); } if (Position.X < 0) { Position = new Vector2(canvasWidth, Position.Y); } if (Position.Y > canvasHeight) { Position = new Vector2(Position.X, 0); } if (Position.Y < 0) { Position = new Vector2(Position.X, canvasHeight); } } }
public Vector2 GetGunPosition() { return(VMath.AddVectors(Position, VMath.ScalarMultiply(UnitDirectionVector, 12))); }
public void Rotate(float degrees) { UnitDirectionVector = VMath.RotateVector(UnitDirectionVector, degrees); }
public void Update() { if (IsUpButtonPressed) { if (player.Speed < 0) { player.Speed = 0; } if (player.Speed < player.MAX_SPEED) { player.Speed++; } } else { if (player.Speed > 0) { player.Speed--; } } if (IsDownButtonPressed) { if (player.Speed > 0) { player.Speed = 0; } if (player.Speed > -player.MAX_SPEED) { player.Speed--; } } else { if (player.Speed < 0) { player.Speed++; } } if (IsLeftButtonPressed) { player.Rotate(player.ROTATE_DEGREE_INCREMENT); } if (IsRightButtonPressed) { player.Rotate(-player.ROTATE_DEGREE_INCREMENT); } if (IsSpaceBarPressed) { App.soundPlayer.Play(); if (player.LastFired == player.FIRE_COOLDOWN && !player.IsDead()) { player.LastFired = 0; Bullet newBullet = new Bullet(VMath.AddVectors(player.Position, new Vector2(rand.Next(8) - 4, rand.Next(8) - 4)), player.UnitDirectionVector, 20, 3); gameObjects.Add(newBullet); } } if (rand.Next(1, 100) <= 1) //random asteroid spawn chance { Asteroid newAsteroid = new Asteroid(new Vector2(rand.Next(0, canvasWidth), 0), new Vector2(rand.Next(-180, 180), rand.Next(-180, 180)), rand.Next(1, 5), rand.Next(15, 100)); gameObjects.Add(newAsteroid); } foreach (var obj in gameObjects.Where((h => (h is HealthBar))).ToList()) { gameObjects.Remove(obj); } HealthBar gameHealthBar = new HealthBar(new Vector2(canvasWidth, canvasHeight), new Vector2(0, 0), 0, player.Health); gameObjects.Add(gameHealthBar); foreach (var obj in gameObjects.ToList()) { obj.Move(canvasWidth, canvasHeight); if (obj.IsDead()) { gameObjects.Remove(obj); } } foreach (var obj in gameObjects.Where((b => (b is Bullet))).ToList()) { if (obj.Position.X < 0 || obj.Position.X > canvasWidth || obj.Position.Y < 0 || obj.Position.Y > canvasHeight) { obj.Destroy(); } } foreach (var obj in gameObjects.Where((a => (a is Asteroid))).ToList()) { if ((obj.Position.X + obj.Radius > player.Position.X && obj.Position.X - obj.Radius < player.Position.X) && (obj.Position.Y + obj.Radius > player.Position.Y && obj.Position.Y - obj.Radius < player.Position.Y)) { if (!player.IsDead()) { player.AsteroidCollision(obj); obj.Destroy(); } } } foreach (var ast in gameObjects.Where((a => (a is Asteroid))).ToList()) { foreach (var bul in gameObjects.Where((b => (b is Bullet))).ToList()) { if ((ast.Position.X + ast.Radius > bul.Position.X && ast.Position.X - ast.Radius < bul.Position.X) && (ast.Position.Y + ast.Radius > bul.Position.Y && ast.Position.Y - ast.Radius < bul.Position.Y)) { gameObjects.Add((ast as Asteroid).Half()); bul.Destroy(); } } } if (player.Health <= 0) { gameOver = true; } }