I am working on a simple game loop.
The idea is to have a parent game object from which all other objects are derrived. The idea behind this is so the class can be expanded and reimplemented for objects such as detail props, players, vehicles, pickups etc; all which can pass around IDs to each other and have their own unique step functions.
Here is a condensed form of my current code with only what applies to the problem:.Code:
#include <iostream>
#include <vector>
#include <cmath>
#include <windows.h>
/*
Parent class to all objects which are part of the main game loop.
Examples:
static props
physics props
players
enemies
vehicles
explosion handlers
triggers
*/
class CgameObject{
public:
long x,y; //location
bool awake, visible;
//constructor
CgameObject(long setX,long setY){
x = setX;
y = setY;
visible = true;
awake = true;
}
virtual void step(){};
};
class CmotionObject : public CgameObject{
public:
double direction, //angle in degrees going anticlockwise from the +ve x axis
speed, //speed in pixels/frame along the direction (+ve or -ve)
vSpeed, //speed in pixels/frame along the y axis, down being +ve
hSpeed; //speed in pixels/frame along the x axis, right being +ve
public:
//constructor
CmotionObject(long setX,long setY):CgameObject(setX,setY){
vSpeed = 0;
hSpeed = 0;
speed = 0;
direction = 0;
}
virtual void applyMotion(){
x += (long)hSpeed;
y += (long)vSpeed;
}
};
using namespace std;
unsigned int i;
unsigned long frameCount;
//#include "classes/gameObject.h"
//#include "classes/motionObject.h"
vector <CgameObject> gameObjects;
class Cplayer : public CmotionObject{
public:
Cplayer(long setX,long setY):CmotionObject(setX,setY){};
//do this every frame
virtual void step(){
applyMotion();
cout << "player is at " << x << endl;
if(x < 100){
if(x >= 100) cout << frameCount << ", player has reached the finishing line\n";
hSpeed = 0; //stop moving
}
}
};
int main(){
frameCount = 0;
gameObjects.push_back(Cplayer(0,30));
while(true){
for(i = 0;i < gameObjects.size();i ++){
gameObjects[i].step();
}
frameCount ++;
cout << "frame #" << frameCount << " completed\n";
Sleep(100); //10 fps
}
return 0;
}
Is this the intended way to do something like this? And in what way have I done it wrong? Thanks and I look forward to the responses.
Also, whilst programming this I noticed things that should be easier to implement. Such as applyMotion() each frame. Is there a way I can make CmotionObject have a step event which calls applyMotion, but step events of CmotionObject's child classes will not overwrite it - but simply perform before it? (or after it)
edit:
A litte more of what I am on about:
If I try to for example: change a variable in an object of the player class, such as player name... it would work something along the lines of gameObjects[0].name = inputFromSomewhere. But it cannot be done because gameObjects is a vector of type CgameObject... I thought the point with polymorphism was that I could do things like that and it would work, unless I landed on an object which was not of the player sub class, such as gameObjects[31].name, if the object of id 31 was a pickup or something. Also, my sleeping system would simply be implemented within the main loop, should I add another data structure of flags? Also, if items are removed from the vector, their indexes will not be preserved and that will ruin things, should I find another way to go about this or is there something else I need to add to get proper functionality when I begin to program something more interesting?
edit:
title isn't very good. If a mod sees this can you change it to Polymorphism within objects in a vector? Or whatever you think is more appropriate, thanks and sorry to have to ask that.