diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/bullet.h | 24 | ||||
-rw-r--r-- | include/flyingObject.h | 28 | ||||
-rw-r--r-- | include/game.h | 54 | ||||
-rw-r--r-- | include/point.h | 49 | ||||
-rw-r--r-- | include/rocks.h | 84 | ||||
-rw-r--r-- | include/ship.h | 32 | ||||
-rw-r--r-- | include/uiDraw.h | 135 | ||||
-rw-r--r-- | include/uiInteract.h | 131 | ||||
-rw-r--r-- | include/velocity.h | 23 |
9 files changed, 560 insertions, 0 deletions
diff --git a/include/bullet.h b/include/bullet.h new file mode 100644 index 0000000..3392ee4 --- /dev/null +++ b/include/bullet.h @@ -0,0 +1,24 @@ +#ifndef bullet_h +#define bullet_h + +#define BULLET_SPEED 10 +#define BULLET_LIFE 40 + +#include "point.h" +#include "velocity.h" +#include "flyingObject.h" + +class Bullet : public FlyingObject +{ +private: + int framesAlive; + +public: + Bullet( const Point &point ); + int getFramesAlive() const { return framesAlive; }; + void fire( const Velocity &velocity , const float angle ); + void advance(); +}; + + +#endif /* bullet_h */ diff --git a/include/flyingObject.h b/include/flyingObject.h new file mode 100644 index 0000000..3296c5a --- /dev/null +++ b/include/flyingObject.h @@ -0,0 +1,28 @@ +#ifndef flyingObject_h +#define flyingObject_h + +#include "point.h" +#include "velocity.h" +#include "uiDraw.h" + +class FlyingObject +{ +protected: + Point point; + Velocity velocity; + bool alive; +public: + FlyingObject() : point( Point() ) , velocity( Velocity() ) , alive( true ) {} + Point getPoint() const { return this->point; } + void setPoint( const Point &point ) { this->point = point; } + Velocity getVelocity() const { return this->velocity; } + void setVelocity( const Velocity &velocity) { this->velocity = velocity; } + bool isAlive() { return this->alive; } + void kill() { alive = false; }; + virtual void draw() { drawDot( point ); }; + virtual void advance(); +}; + + + +#endif /* flyingObject_h */ diff --git a/include/game.h b/include/game.h new file mode 100644 index 0000000..943ed63 --- /dev/null +++ b/include/game.h @@ -0,0 +1,54 @@ +/********************************************************************* + * File: game.h + * Description: Defines the game class for Asteroids + * + *********************************************************************/ + +#ifndef GAME_H +#define GAME_H + +#include <vector> +#include "bullet.h" +#include "ship.h" +#include "rocks.h" +#include "uiDraw.h" +#include "uiInteract.h" + +#define CLOSE_ENOUGH 15 + +using namespace std; + +class Game +{ +private: + Point topLeft; + Point bottomRight; + vector<Rock *> rocks; + vector<Bullet *> bullets; + Ship* ship; + int level = 0; + + float getClosestDistance( const FlyingObject &obj1 , const FlyingObject &obj2 ) const; + + vector<Rock *> createRocks(); + + void advanceBullets(); + void advanceAsteroids(); + void advanceShip(); + + void cleanUpZombies(); + + void handleCollisions(); + + void drawBullets(); + void drawRocks(); + void drawShip(); +public: + Game ( const Point &tl , const Point &br ); + void handleInput ( const Interface &ui ); + void draw ( const Interface &ui ); + void advance(); +}; + + +#endif /* GAME_H */ diff --git a/include/point.h b/include/point.h new file mode 100644 index 0000000..7f0715f --- /dev/null +++ b/include/point.h @@ -0,0 +1,49 @@ +/*********************************************************************** + * Header File: + * Point : The representation of a position on the screen + * Author: + * Br. Helfrich + * Summary: + * Everything we need to know about a location on the screen, including + * the location and the bounds. + ************************************************************************/ + + +#ifndef POINT_H +#define POINT_H + +#include <iostream> + +/********************************************* + * POINT + * A single position. + *********************************************/ +class Point +{ +public: + // constructors + Point() : x(0.0), y(0.0) {} + Point(bool check) : x(0.0), y(0.0) {} + Point(float x, float y); + + // getters + float getX() const { return x; } + float getY() const { return y; } + + // setters + void setX(float x); + void setY(float y); + void addX(float dx) { setX(getX() + dx); } + void addY(float dy) { setY(getY() + dy); } + + bool inRange ( const Point &p , const float range ); +private: + float x; // horizontal position + float y; // vertical position +}; + +// stream I/O useful for debugging +std::ostream & operator << (std::ostream & out, const Point & pt); +std::istream & operator >> (std::istream & in, Point & pt); + +#endif // POINT_H diff --git a/include/rocks.h b/include/rocks.h new file mode 100644 index 0000000..ae12e2b --- /dev/null +++ b/include/rocks.h @@ -0,0 +1,84 @@ +#ifndef rocks_h +#define rocks_h + +#define BIG_ROCK_SIZE 16 +#define MEDIUM_ROCK_SIZE 8 +#define SMALL_ROCK_SIZE 4 + +#define BIG_ROCK_SPIN 2 +#define MEDIUM_ROCK_SPIN 5 +#define SMALL_ROCK_SPIN 10 + +#include "flyingObject.h" +#include <vector> + +using namespace std; + +class Rock : public FlyingObject +{ +protected: + int rotationDegPerFrame; + int size; + float angle; +public: + Rock() : FlyingObject() , rotationDegPerFrame ( 0 ) , size ( 1 ) , angle ( 0 ) {} + Rock ( int degPerFrame , int size , float angle ) : rotationDegPerFrame ( degPerFrame ) , size ( size ) , angle ( angle ) {} + + int getRotationDegPerFrame() { return rotationDegPerFrame; } + int getSize () { return size; } + float getAngle() { return angle; } + + void setAngle ( const float angle ) { this->angle = angle; } + void setRotationDegPerFrame ( const int degPerFrame ) { this->rotationDegPerFrame = degPerFrame; } + void setSize ( const int size ) { this->size = size; } + void advance() { this->angle += rotationDegPerFrame; FlyingObject::advance(); } + virtual void draw() {}; + virtual vector<Rock *> destroy() = 0; +}; + +class BigRock : public Rock +{ +public: + BigRock ( const Point &point , const Velocity &velocity ) + { + setPoint ( point ); + setAngle ( 0 ); + setSize ( BIG_ROCK_SIZE ); + setRotationDegPerFrame ( BIG_ROCK_SPIN ); + setAngle ( random ( 0 , 360 ) ); + setVelocity ( velocity ); + } + + vector<Rock *> destroy(); + void draw(); +}; + +class MediumRock : public Rock +{ +public: + MediumRock ( const Point &point , const Velocity &vel ) { + this->point = point; + this->velocity = vel; + setAngle ( 0 ); + setRotationDegPerFrame ( MEDIUM_ROCK_SPIN ); + setSize ( MEDIUM_ROCK_SIZE ); + } + vector<Rock *> destroy(); + void draw(); +}; + +class SmallRock : public Rock +{ +public: + SmallRock( const Point &point , const Velocity &vel ) { + this->point = point; + this->velocity = vel; + setAngle ( 0 ); + setRotationDegPerFrame ( SMALL_ROCK_SPIN ); + setSize ( SMALL_ROCK_SIZE ); + } + vector<Rock *> destroy(); + void draw(); +}; + +#endif /* rocks_h */ diff --git a/include/ship.h b/include/ship.h new file mode 100644 index 0000000..46b6817 --- /dev/null +++ b/include/ship.h @@ -0,0 +1,32 @@ +#ifndef ship_h +#define ship_h + +#define SHIP_SIZE 10 +#define ROTATE_AMOUNT 6 +#define THRUST_AMOUNT 0.5 + +#include "flyingObject.h" +#include "uiDraw.h" +#include "uiInteract.h" + +using namespace std; + +class Ship : public FlyingObject +{ +private: + float angle; + bool isThrusting; + int fuel; +public: + Ship( const Point &point ) : FlyingObject() , fuel ( 1000 ) , angle ( 90.0 ) , isThrusting( false ) { setPoint ( point ); } + float getAngle() const { return this->angle; } + float getFuel() { return this->fuel; } + void setFuel ( const int fuel ) { this->fuel - fuel; } + void thrust( const bool isUp ); + void rotate( const bool isRight ); + bool getIsThrusting() { return isThrusting; } + void setThrusting( const bool isThrusting ) { this->isThrusting = isThrusting; } + void draw() const; +}; + +#endif /* ship_h */ diff --git a/include/uiDraw.h b/include/uiDraw.h new file mode 100644 index 0000000..caa0fb5 --- /dev/null +++ b/include/uiDraw.h @@ -0,0 +1,135 @@ +/*********************************************************************** + * Header File: + * User Interface Draw : put pixels on the screen + * Author: + * Br. Helfrich + * Summary: + * This is the code necessary to draw on the screen. We have a collection + * of procedural functions here because each draw function does not + * retain state. In other words, they are verbs (functions), not nouns + * (variables) or a mixture (objects) + ************************************************************************/ + +#ifndef UI_DRAW_H +#define UI_DRAW_H + +#include <string> // To display text on the screen +#include <cmath> // for M_PI, sin() and cos() +#include "point.h" // Where things are drawn +using std::string; + +/************************************************************************ + * DRAW DIGIT + * Draw a single digit in the old school line drawing style. The + * size of the glyph is 8x11 or x+(0..7), y+(0..10) + *************************************************************************/ +void drawDigit(const Point & topLeft, char digit); + +/************************************************************************* + * DRAW NUMBER + * Display an integer on the screen using the 7-segment method + *************************************************************************/ +void drawNumber(const Point & topLeft, int number); + +/************************************************************************* + * DRAW TEXT + * Draw text using a simple bitmap font + ************************************************************************/ +void drawText(const Point & topLeft, const char * text); + +/************************************************************************ + * ROTATE + * Rotate a given point (point) around a given origin (center) by a given + * number of degrees (angle). + *************************************************************************/ +void rotate(Point & point, const Point & origin, int rotation = 0); + +/************************************************************************ + * DRAW RECTANGLE + * Draw a rectangle on the screen centered on a given point (center) of + * a given size (width, height), and at a given orientation (rotation) + * measured in degrees (0 - 360) + *************************************************************************/ +void drawRect(const Point & center, int width, int height, int rotation); + +/************************************************************************ + * DRAW CIRCLE + * Draw a circle from a given location (center) of a given size (radius). + *************************************************************************/ +void drawCircle(const Point & center, int radius); + +/************************************************************************ + * DRAW POLYGON + * Draw a polygon from a given location (center) of a given size (radius). + *************************************************************************/ +void drawPolygon(const Point & center, + int radius = 20, + int points = 4, + int rotation = 0); + +/************************************************************************ + * DRAW LINE + * Draw a line on the screen from the beginning to the end. + *************************************************************************/ +void drawLine(const Point & begin, const Point & end, + float red = 1.0, float green = 1.0, float blue = 1.0); + + +/*********************************************************************** + * DRAW Lander + * Draw a moon-lander spaceship on the screen at a given point + ***********************************************************************/ +void drawLander(const Point & point); + +/*********************************************************************** + * DRAW Lander Flame + * Draw the flames coming out of a moonlander for thrust + ***********************************************************************/ +void drawLanderFlames(const Point & point, + bool bottom, + bool left, + bool right); + +/************************************************************************ + * DRAW DOT + * Draw a single point on the screen, 2 pixels by 2 pixels + *************************************************************************/ +void drawDot(const Point & point); + +/************************************************************************ + * DRAW Sacred Bird + * Draw the bird on the screen + *************************************************************************/ +void drawSacredBird(const Point & center, float radius); + +/************************************************************************ + * DRAW Tough Bird + * Draw a tough bird on the screen + *************************************************************************/ +void drawToughBird(const Point & center, float radius, int hits); + +/************************************************************************ + * DRAW Ship + * Draw the spaceship on the screen + *************************************************************************/ +void drawShip(const Point & point, int rotation, bool thrust); + +/********************************************************************** + * DRAW * ASTEROID + **********************************************************************/ +void drawSmallAsteroid( const Point & point, int rotation); +void drawMediumAsteroid(const Point & point, int rotation); +void drawLargeAsteroid( const Point & point, int rotation); + +/****************************************************************** + * RANDOM + * This function generates a random number. The user specifies + * The parameters + * INPUT: min, max : The number of values (min <= num <= max) + * OUTPUT <return> : Return the integer + ****************************************************************/ +int random(int min, int max); +double random(double min, double max); + + +#endif // UI_DRAW_H diff --git a/include/uiInteract.h b/include/uiInteract.h new file mode 100644 index 0000000..d7bfe6b --- /dev/null +++ b/include/uiInteract.h @@ -0,0 +1,131 @@ +/********************************************* + * Header file: + * UI INTERFACE + * Author: + * Br. Helfrich + * Summary: + * This module will create an OpenGL window, + * enter the OpenGL main loop, and accept events. + * The main methods are: + * 1. Constructors - Create the window + * 2. run() - Run the main loop + * 3. callback - Specified in Run, this user-provided + * function will get called with every frame + * 4. isDown() - Is a given key pressed on this loop? + **********************************************/ + +#ifndef UI_INTERFACE_H +#define UI_INTERFACE_H + + #include "point.h" + +/******************************************** + * INTERFACE + * All the data necessary to keep our graphics + * state in memory + ********************************************/ +class Interface +{ +public: + // Default constructor useful for setting up the random variables + // or for opening the file for output + Interface() { initialize(0, 0x0000, "Window", Point(-50, 50), Point(50, -50)); }; + + // Constructor if you want to set up the window with anything but + // the default parameters + Interface(int argc, char ** argv, const char * title, Point topLeft, Point bottomRight) + { + initialize(argc, argv, title, topLeft, bottomRight); + } + + // Destructor, incase any housecleaning needs to occr + ~Interface(); + + // This will set the game in motion + void run(void (*callBack)(const Interface *, void *), void *p); + + // Is it time to redraw the screen + bool isTimeToDraw(); + + // Set the next draw time based on current time and time period + void setNextDrawTime(); + + // Retrieve the next tick time... the time of the next draw. + unsigned int getNextTick() { return nextTick; }; + + // How many frames per second are we configured for? + void setFramesPerSecond(double value); + + // Key event indicating a key has been pressed or not. The callbacks + // should be the only onces to call this + void keyEvent(int key, bool fDown); + void keyEvent(); + + // Current frame rate + double frameRate() const { return timePeriod; }; + + // Get various key events + int isDown() const { return isDownPress; }; + int isUp() const { return isUpPress; }; + int isLeft() const { return isLeftPress; }; + int isRight() const { return isRightPress; }; + bool isSpace() const { return isSpacePress; }; + + static void *p; // for client + static void (*callBack)(const Interface *, void *); + +private: + void initialize(int argc, char ** argv, const char * title, Point topLeft, Point bottomRight); + + static bool initialized; // only run the constructor once! + static double timePeriod; // interval between frame draws + static unsigned int nextTick; // time (from clock()) of our next draw + + static int isDownPress; // is the down arrow currently pressed? + static int isUpPress; // " up " + static int isLeftPress; // " left " + static int isRightPress; // " right " + static bool isSpacePress; // " space " +}; + + + +/************************************************************************ + * DRAW CALLBACK + * This is the main callback from OpenGL. It gets called constantly by + * the graphics engine to refresh and draw the window. Here we will + * clear the background buffer, draw on it, and send it to the forefront + * when the appropriate time period has passsed. + * + * Note: This and all other callbacks can't be member functions, they must + * have global scope for OpenGL to see them. + *************************************************************************/ +void drawCallback(); + +/************************************************************************ + * KEY DOWN CALLBACK + * When a key on the keyboard has been pressed, we need to pass that + * on to the client. Currnetly, we are only registering the arrow keys + *************************************************************************/ +void keyDownCallback(int key, int x, int y); + +/************************************************************************ + * KEY UP CALLBACK + * When the user has released the key, we need to reset the pressed flag + *************************************************************************/ +void keyUpCallback(int key, int x, int y); + +/*************************************************************** + * KEYBOARD CALLBACK + * Generic callback to a regular ascii keyboard event, such as + * the space bar or the letter 'q' + ***************************************************************/ +void keyboardCallback(unsigned char key, int x, int y); + +/************************************************************************ + * RUN + * Set the game in action. We will get control back in our drawCallback + *************************************************************************/ +void run(); + +#endif // UI_INTERFACE_H diff --git a/include/velocity.h b/include/velocity.h new file mode 100644 index 0000000..11c41cb --- /dev/null +++ b/include/velocity.h @@ -0,0 +1,23 @@ +#ifndef VELOCITY_H +#define VELOCITY_H + +#include "point.h" + +class Velocity +{ + public: + Velocity(); + Velocity( float dx , float dy ); + float getDx() const; + float getDy() const; + void addDy( const float dy ); + void addDx( const float dx ); + void setDx( float dx ); + void setDy( float dy ); + Point updatePoint ( Point &point ); + private: + float dx; + float dy; +}; + +#endif |