From 0f4b32440b28fcc7c29139b86d17aa1d32ed2518 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 28 Sep 2008 11:25:56 +0100 Subject: [PATCH] Fix missiles to the side --- data/Makefile.am | 2 +- data/images/missile.png | Bin 0 -> 406 bytes src/Game.cpp | 10 ++++++++ src/Game.hpp | 6 +++++ src/Makefile.am | 2 +- src/Missile.cpp | 49 ++++++++++++++++++++++++++++++++++++++++ src/Missile.hpp | 41 +++++++++++++++++++++++++++++++++ src/ObjectGrid.cpp | 8 +++++++ src/ObjectGrid.hpp | 2 ++ 9 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 data/images/missile.png create mode 100644 src/Missile.cpp create mode 100644 src/Missile.hpp diff --git a/data/Makefile.am b/data/Makefile.am index ed6563c..66a2926 100644 --- a/data/Makefile.am +++ b/data/Makefile.am @@ -7,7 +7,7 @@ EXTRA_DIST = images/ship.png images/star.png images/start_option.png \ images/mine.png images/levelcomp.png images/shipsmall.png \ images/gateway.png images/hscore.png images/red_rock_surface.png \ images/rock_surface.png images/snow_surface.png \ - images/dirt_surface.png \ + images/dirt_surface.png images/missile.png \ images/red_rock_surface2.png images/rock_surface2.png \ images/snow_surface2.png images/dirt_surface2.png \ images/fuelmeter.png \ diff --git a/data/images/missile.png b/data/images/missile.png new file mode 100644 index 0000000000000000000000000000000000000000..0051d44472e3d211cae96010112ea8c11a672d0a GIT binary patch literal 406 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}Y)RhkE)4%c zaKYZ?lYt_f1s;*b3=G`DAk4@xYmNj^kiEpy*OmPSrwpgO#Fs|l&p;u`64!_l=ltB< z)VvY~=c3falGGH1^30M91$R&1fbd2>aiF4)o-U3d5r^MS-^klyz~frK==DSa*6tQw z359M0CRS0?ZkH=L5Bg@gzNm?9c~B(o?(t$tji#%@$NFuvBMgm$uA1;NGYGDHqHt!; zmL9`B>$LN4A3G-D*Sk2_c>c1J@1IwGyE?&Z`HcR@6+idscnh^0JACQVo8SBR|EVh# zivC;8B&ooh(!eu;(a3@A1VcZQ$GoUXo2|?qe3bm>eQsw4NA32VB9Bx}<=Y&2KIWDL zFTD97=U%Dow_0|FE2X>)8eL2b0n=0&7Oc{;Ut;n6Pi%H>LEV+|X|_vct&aVfQQLEC tTkBRcu1A-e{M$P`Jk{r2m|LuUFUS5&bLE| MineList; typedef MineList::iterator MineListIt; MineList mines; + + // Missiles + typedef vector MissileList; + typedef MissileList::iterator MissileListIt; + MissileList missiles; }; #endif diff --git a/src/Makefile.am b/src/Makefile.am index 7eceb33..396b204 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,7 @@ lander_SOURCES = Main.cpp Game.hpp Game.cpp \ Ship.hpp Ship.cpp Viewport.hpp Viewport.cpp LandingPad.hpp \ LandingPad.cpp Surface.hpp Surface.cpp Mine.hpp Mine.cpp \ ElectricGate.hpp ElectricGate.cpp Key.hpp Key.cpp \ - Geometry.hpp Image.hpp \ + Geometry.hpp Image.hpp Missile.hpp Missile.cpp \ Image.cpp AnimatedImage.hpp AnimatedImage.cpp \ Texture.hpp Texture.cpp Options.hpp Options.cpp \ ConfigFile.hpp ConfigFile.cpp SoundEffect.hpp \ diff --git a/src/Missile.cpp b/src/Missile.cpp new file mode 100644 index 0000000..e7f4a30 --- /dev/null +++ b/src/Missile.cpp @@ -0,0 +1,49 @@ +/* Missile.cpp -- Missiles on asteroids, etc. + * Copyright (C) 2008 Nick Gasson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Missile.hpp" +#include "LoadOnce.hpp" + +Image *Missile::image = NULL; + +Missile::Missile(ObjectGrid *o, Viewport *v, Side s) + : viewport(v), objgrid(o) +{ + // This constructor builds a missile attached to the side of the screen + + LOAD_ONCE { + image = new Image("images/missile.png"); + } + + x = (s == SIDE_LEFT) ? 0 : o->GetWidth() - 1; + + // Pick spaces at random until we find one that's empty + do { + y = rand() % o->GetHeight(); + } while (o->IsFilled(x, y)); +} + +void Missile::Draw() const +{ + if (viewport->ObjectInScreen(x, y, 1, 1)) { + int dx, dy; + ObjectGrid::Offset(x, y, &dx, &dy); + + image->Draw(dx - viewport->GetXAdjust(), + dy - viewport->GetYAdjust()); + } +} diff --git a/src/Missile.hpp b/src/Missile.hpp new file mode 100644 index 0000000..785b686 --- /dev/null +++ b/src/Missile.hpp @@ -0,0 +1,41 @@ +/* Missile.hpp -- Missiles on asteroids, etc. + * Copyright (C) 2008 Nick Gasson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef INC_MISSILE_HPP +#define INC_MISSILE_HPP + +#include "ObjectGrid.hpp" +#include "Viewport.hpp" +#include "Image.hpp" + +class Missile { +public: + enum Side { SIDE_LEFT, SIDE_RIGHT }; + + Missile(ObjectGrid *o, Viewport *v, Side s); + + void Draw() const; +private: + Viewport *viewport; + ObjectGrid *objgrid; + int x, y; + double angle; + + static Image *image; +}; + +#endif diff --git a/src/ObjectGrid.cpp b/src/ObjectGrid.cpp index c29c8d7..79c9596 100644 --- a/src/ObjectGrid.cpp +++ b/src/ObjectGrid.cpp @@ -126,6 +126,14 @@ bool ObjectGrid::IsFilled(int x, int y) const return grid[x + (width * y)]; } +void ObjectGrid::Offset(int ox, int oy, int *x, int *y) +{ + // Find the absolute coordinates of object grid point + // (ox, oy) and store them in (x, y) + *x = ox*OBJ_GRID_SIZE; + *y = OBJ_GRID_TOP + oy*OBJ_GRID_SIZE; +} + bool StaticObject::ObjectInScreen(Viewport *viewport) { return viewport->ObjectInScreen(xpos, ypos, width, height); diff --git a/src/ObjectGrid.hpp b/src/ObjectGrid.hpp index f5dc795..20d6b06 100644 --- a/src/ObjectGrid.hpp +++ b/src/ObjectGrid.hpp @@ -36,6 +36,8 @@ public: int GetWidth() const { return width; } int GetHeight() const { return height; } + static void Offset(int ox, int oy, int *x, int *y); + static const int OBJ_GRID_SIZE = 32; static const int OBJ_GRID_TOP = 100; -- 2.39.2