Hello all, I have just begun ( quite literally ) writing a two dimensional space adventure game.
I know I'll need help with it, but I also think that some of the methods I use will be helpful to those who are just starting out.
Therefore, if in accordance with the forum rules, I am going to slowly fill this thread with my progress on the game and include
all the code used. Feel free to use any of the code, and feel especially free to criticize or suggest improvements. I think that this would be a great way for everyone to learn a bit ^^.
I would have posted this in the WIP forums, but since I want to start this from almost the very beginning, I decided it would fit better here. Once the game is sufficiently well on it's way, I will ask the thread to be moved, or create a new thread in the WIP forum.
Anyway, I'll get this started with what I have so far. Again, please suggest improvements if you have them.
Here is the basic design doc so everyone has an idea of what I'm going for.
Quote: "
Game Design Doc for Project Eres
Tags:
2d, Space Opera, Freeform, Newtonian, Exploration, Battles, Trading, Conquest
Premise:
You shove off into space in a brand new [rookie ship] with nothing but the clothes on your back and a few credits. You are free to do as you choose, become a lethal pirate, peaceful trader, smuggler, or military captain, grunt, or try them all!
Definite Features:
- Newtonian physics, you continue moving in the direction you started, frictionless movement and torque. Weapons should have recoil, except for light based laser weapons.
- Large explorable universe, everything should be at realistic distances from eachother. Several methods of faster-than-light travel should be available to minimize travelling time.
- Time compression for long journeys
- Convincing AI. There should be a number of different sub-AI's per each main type. e.g. not all smugglers should have the exact same programming. On a side note, smugglers should not be advertised as such.
- NO ship to ship collisions, as it is a 2d game and collisions would be far more frequent than would be realistic. The same goes for any sort of space debris.
- Player should be able to modify his vessel with different equipment. Each ship should have a number of 'hardpoints' for different types of equipment, similar to EVE-Online.
- NPC ships should be UNIQUE, all NPC's should have a name and a variety of different fittings for their ships. AKA if Ship Chassis 'Raven' is fitted with two blasters and a missile launcher by default, some NPC's should have 3 blasters and no missile launcher, some should have the default, and some should have all missile launchers.
- If the player dies, he is dead for good. Saving is enabled, but the save file will be deleted if the player is killed. Notice that being killed is different than simply losing your ship.
- Player should be able to eject from his ship to save himself. However, certain opponents may 'pod-kill' the player's escape pod, permanately killing him.
- Player should be able to hire escorts, give them orders, and even tell them to travel long distances alone.
- It should be possible to loot or capture disabled ships, or destroy them and all crew. Upon the successful capture of a ship, options will be given to either add it to your fleet or take command of it yourself. Should the player choose to take command, his previous ship will be added to fleet.
- Player can store items in hangers at various stations, and pay NPCs to transport them. The NPCs might get attacked by pirates, so player should also be able to pay for protection or protect them himself.
- Wormholes / Blackholes should exist. If the player enters or is drawn into one, results should vary. He may be transported to a specific point on the map, destroyed, damaged, or any number of wierd outcomes. Parallel dimensions where everything is different? An entirely different universe?
Possible Features:
- Support for boarding ships?
- Randomly generated universe?
- Moving planets?
- Destructible planets / stations?
- Exiting ship to walk around on ground?
"
Now that you have the basic idea, here is all the code I have so far. It made up of 8 source files so far.
Here is our Main.cpp
// Prototypes
void Draw_Spaceships( );
// This function draws the screen
void Draw_Screen( )
{
// Draw spaceships
Draw_Spaceships( );
// Refreshes the screen
dbSync( );
// Clears the screen
dbCLS( );
}
// This function will draw all the spaceships if they are on the screen
void Draw_Spaceships( )
{
if ( TestShip.Pos.x > 0 && TestShip.Pos.x < dbScreenWidth( )
&& TestShip.Pos.y > 0 && TestShip.Pos.y < dbScreenHeight( ) )
{
dbSprite( TestShip.SpriteID, TestShip.Pos.x, TestShip.Pos.y, TestShip.ImageID );
dbOffsetSprite( TestShip.SpriteID, dbSpriteWidth( TestShip.SpriteID ) / 2, dbSpriteHeight( TestShip.SpriteID ) / 2 );
}
}
Here is my BasicClasses.h file, which has some objects that the game will use frequently.
class PointObject
{
public:
float x;
float y;
};
class CircleObject
{
public:
float x;
float y;
float Radius;
};
class BoxObject
{
public:
float x;
float y;
float Width;
float Height;
};
Next is our Utilities.h file. It's mostly empty right now.
// This contains useful functions for the game
int FreeSprite( )
{
int i = 1;
while ( dbSpriteExist( i ) )
{
i += 1;
}
return i;
}
Now the SpaceshipClass.h. This class will have a lot of work done on it, but this is some of the stuff I threw into it for now.
class SpaceshipClass
{
public:
PointObject Pos;
PointObject Vector;
PointObject OldPos;
float Angle;
float Torque;
int SpriteID;
int ImageID;
int GunHardpoints;
int TurretHardpoints;
int MissileHardpoints;
SpaceshipClass( );
void Update( );
};
SpaceshipClass::SpaceshipClass( )
{
Pos.x = 0;
Pos.y = 0;
Vector.x = 0;
Vector.y = 0;
OldPos.x = 0;
OldPos.y = 0;
Angle = 0;
Torque = 0;
SpriteID = 0;
ImageID = 0;
}
void SpaceshipClass::Update( )
{
Pos.x += Vector.x;
Pos.y += Vector.y;
Angle += Torque;
}
The Setup.h file, which I use to initialize things.
#include "DarkGDK.h"
// Create Ships
SpaceshipClass TestShip;
// This function will setup the screen
void Init_Screen( )
{
dbSetDisplayMode( 800, 600, 32 );
dbSyncOn( );
dbSyncRate( 60 );
}
// This function loads all the images
void Load_Images( )
{
dbLoadImage( "Media/TestShip.png", 1 );
}
// This function initalizes all the spaceship values
void Setup_Spaceships( )
{
TestShip.ImageID = 1;
TestShip.SpriteID = FreeSprite( );
TestShip.Pos.x = 400;
TestShip.Pos.y = 300;
}
The Graphics.h file, which I'll use to draw things.
// Prototypes
void Draw_Spaceships( );
// This function draws the screen
void Draw_Screen( )
{
// Draw spaceships
Draw_Spaceships( );
// Refreshes the screen
dbSync( );
// Clears the screen
dbCLS( );
}
// This function will draw all the spaceships if they are on the screen
void Draw_Spaceships( )
{
if ( TestShip.Pos.x > 0 && TestShip.Pos.x < dbScreenWidth( )
&& TestShip.Pos.y > 0 && TestShip.Pos.y < dbScreenHeight( ) )
{
dbSprite( TestShip.SpriteID, TestShip.Pos.x, TestShip.Pos.y, TestShip.ImageID );
dbOffsetSprite( TestShip.SpriteID, dbSpriteWidth( TestShip.SpriteID ) / 2, dbSpriteHeight( TestShip.SpriteID ) / 2 );
}
}
Collision.h has some simple functions that I use in a lot of projects. It's not perfect but it works ok.
bool CollidePointBox( PointObject Point, BoxObject Box )
{
bool Collide = false;
if ( Point.x > Box.x && Point.x < Box.x + Box.Width &&
Point.y > Box.y && Point.y < Box.y + Box.Height )
{
Collide = true;
}
return Collide;
}
bool CollidePointCircle( PointObject Point, CircleObject Circle )
{
bool Collide = false;
if ( sqrt( powf( Point.x - Circle.x, 2 ) + powf( Point.y - Circle.y, 2 ) ) < Circle.Radius )
{
Collide = true;
}
return Collide;
}
bool CollideCircleCircle( CircleObject CircleA, CircleObject CircleB )
{
bool Collide = false;
if ( sqrt( powf( CircleA.x - CircleB.x, 2 ) + powf( CircleA.y - CircleB.y, 2 ) ) < (CircleA.Radius) + (CircleB.Radius) )
{
Collide = true;
}
return Collide;
}
GameMechanics.h will grow quite a bit as well. Right now it just calls the Update routine on our test ship.
// This file contains functions which define the game engine
// This function will update ALL the spaceships
void Update_Spaceships( )
{
TestShip.Update( );
}
To compile you'll need a 'TestShip.png" file. I've attached the image for your use, it's just a random spaceship sprite I found on google. Once the engine comes along more I'll make real spaceships that look nicer.
And that's all of it! If you compile, you'll get a spaceship sitting in the center of the screen, which doesn't move, or do much of anything really. Hopefully we'll get it moving in the next post.
I look forward to seeing your opinions about my coding style and the like ^^.
~Phos