From 1fea34901507be2b98296457f15658490ba64fb9 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 28 Sep 2008 12:31:00 +0100 Subject: [PATCH] Fire missiles when near player --- src/Game.cpp | 4 ++++ src/Missile.cpp | 45 ++++++++++++++++++++++++++++++++++++++++----- src/Missile.hpp | 14 ++++++++++++-- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 2c26ce1..d85a757 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -190,6 +190,10 @@ void Game::Process() // Move mines for (MineListIt it = mines.begin(); it != mines.end(); ++it) (*it).Move(); + + // Move or fire missiles + for (MissileListIt it = missiles.begin(); it != missiles.end(); ++it) + (*it).Move(ship); // Calculate view adjusts ship.CentreInViewport(); diff --git a/src/Missile.cpp b/src/Missile.cpp index 2fed8b6..e1fa333 100644 --- a/src/Missile.cpp +++ b/src/Missile.cpp @@ -20,8 +20,10 @@ Image *Missile::image = NULL; +const double Missile::ACCEL(0.1); + Missile::Missile(ObjectGrid *o, Viewport *v, Side s) - : viewport(v), objgrid(o) + : viewport(v), objgrid(o), speed(0.0), state(FIXED) { // This constructor builds a missile attached to the side of the screen @@ -35,16 +37,49 @@ Missile::Missile(ObjectGrid *o, Viewport *v, Side s) do { y = rand() % o->GetHeight(); } while (o->IsFilled(x, y)); + + ObjectGrid::Offset(x, y, &dx, &dy); angle = (s == SIDE_LEFT) ? 90 : 270; } void Missile::Draw() const { - if (viewport->ObjectInScreen(x, y, 1, 1)) { - int dx, dy; - ObjectGrid::Offset(x, y, &dx, &dy); - + if (viewport->PointInScreen(dx, dy, ObjectGrid::OBJ_GRID_SIZE, + ObjectGrid::OBJ_GRID_SIZE)) image->Draw(dx - viewport->GetXAdjust(), dy - viewport->GetYAdjust(), angle); +} + +void Missile::Move(const Ship &ship) +{ + switch (state) { + case FIXED: MoveFixed(ship); break; + case FLYING: MoveFlying(); break; + case DESTROYED: MoveDestroyed(); break; } } + +void Missile::MoveFixed(const Ship &ship) +{ + // Hmm... add some fancy logic here to decided when to fire + if (ship.GetY() > dy) + state = FLYING; +} + +void Missile::MoveFlying() +{ + dx += speed * sin(angle * M_PI/180); + dy += speed * cos(angle * M_PI/180); + + speed += ACCEL; + + if (dx > viewport->GetLevelWidth() || dy > viewport->GetLevelHeight() + || dx < 0 || dy < 0) + state = DESTROYED; +} + +void Missile::MoveDestroyed() +{ + +} + diff --git a/src/Missile.hpp b/src/Missile.hpp index 785b686..c89aa1d 100644 --- a/src/Missile.hpp +++ b/src/Missile.hpp @@ -21,6 +21,7 @@ #include "ObjectGrid.hpp" #include "Viewport.hpp" #include "Image.hpp" +#include "Ship.hpp" class Missile { public: @@ -29,13 +30,22 @@ public: Missile(ObjectGrid *o, Viewport *v, Side s); void Draw() const; + void Move(const Ship &ship); private: + void MoveFixed(const Ship &ship); + void MoveFlying(); + void MoveDestroyed(); + Viewport *viewport; ObjectGrid *objgrid; - int x, y; - double angle; + int x, y, dx, dy; + double angle, speed; + + enum State { FIXED, FLYING, DESTROYED }; + State state; static Image *image; + static const double ACCEL; }; #endif -- 2.39.2