From d02ee4dd9869c9913d64ab62ede649d7d7cfa9b8 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Tue, 18 Nov 2008 18:26:46 +0000 Subject: [PATCH] Check for collisions with missiles --- src/Game.cpp | 15 ++++++++++++++- src/Missile.cpp | 7 +++++++ src/Missile.hpp | 1 + src/Ship.cpp | 6 +++--- src/Ship.hpp | 6 +++--- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index d85a757..b69b4be 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -270,6 +270,19 @@ void Game::Process() } } + // Check for collisons with missiles + for (MissileListIt it = missiles.begin(); it != missiles.end(); ++it) { + if ((*it).CheckCollison(ship)) { + if (state == gsInGame) { + // Destroy the ship + ExplodeShip(); + ship.Bounce(); + } + else if (state == gsExplode) + EnterDeathWait(); + } + } + // See if the player collected a key for (KeyListIt it = keys.begin(); it != keys.end(); ++it) { if((*it).CheckCollision(ship)) { @@ -452,7 +465,7 @@ void Game::StartLevel(int level) // Create missiles missiles.clear(); - for (int i = 0; i < 1; i++) { + for (int i = 0; i < 10; i++) { missiles.push_back(Missile(&objgrid, &viewport, Missile::SIDE_LEFT)); } diff --git a/src/Missile.cpp b/src/Missile.cpp index e1fa333..6f1abbb 100644 --- a/src/Missile.cpp +++ b/src/Missile.cpp @@ -50,6 +50,13 @@ void Missile::Draw() const image->Draw(dx - viewport->GetXAdjust(), dy - viewport->GetYAdjust(), angle); } +bool Missile::CheckCollison(const Ship &ship) +{ + // A bounding box collision isn't exactly accurate but should + // work OK + return ship.BoxCollision(dx, dy, image->GetWidth(), image->GetHeight()); +} + void Missile::Move(const Ship &ship) { switch (state) { diff --git a/src/Missile.hpp b/src/Missile.hpp index c89aa1d..8ad8e4a 100644 --- a/src/Missile.hpp +++ b/src/Missile.hpp @@ -31,6 +31,7 @@ public: void Draw() const; void Move(const Ship &ship); + bool CheckCollison(const Ship &ship); private: void MoveFixed(const Ship &ship); void MoveFlying(); diff --git a/src/Ship.cpp b/src/Ship.cpp index c497e0a..b27601a 100644 --- a/src/Ship.cpp +++ b/src/Ship.cpp @@ -179,7 +179,7 @@ void Ship::RotatePoints(const Point *pPoints, Point *pDest, int nCount, /* * Check for collision between the ship and a polygon. */ -bool Ship::HotSpotCollision(LineSegment &l, double dx, double dy) +bool Ship::HotSpotCollision(LineSegment &l, double dx, double dy) const { for (int i = 0; i < NUM_HOTSPOTS; i++) { if (CheckCollision(l, dx + points[i].x, dy + points[i].y)) @@ -192,7 +192,7 @@ bool Ship::HotSpotCollision(LineSegment &l, double dx, double dy) /* * Checks for collision between the ship and a box. */ -bool Ship::BoxCollision(int x, int y, int w, int h) +bool Ship::BoxCollision(int x, int y, int w, int h) const { if (!viewport->PointInScreen(x, y, w, h)) return false; @@ -209,7 +209,7 @@ bool Ship::BoxCollision(int x, int y, int w, int h) /* * Checks for collision between the ship and a line segment. */ -bool Ship::CheckCollision(LineSegment &l, double dx, double dy) +bool Ship::CheckCollision(LineSegment &l, double dx, double dy) const { double xpos = this->xpos + dx; double ypos = this->ypos + dy; diff --git a/src/Ship.hpp b/src/Ship.hpp index 0e490c0..6eebed7 100644 --- a/src/Ship.hpp +++ b/src/Ship.hpp @@ -44,9 +44,9 @@ public: void ApplyGravity(double gravity); void CentreInViewport(); - bool CheckCollision(LineSegment &l, double dx=0, double dy=0); - bool HotSpotCollision(LineSegment &l, double dx=0, double dy=0); - bool BoxCollision(int x, int y, int w, int h); + bool CheckCollision(LineSegment &l, double dx=0, double dy=0) const; + bool HotSpotCollision(LineSegment &l, double dx=0, double dy=0) const; + bool BoxCollision(int x, int y, int w, int h) const; double GetX() const { return xpos; } double GetY() const { return ypos; } -- 2.39.2