Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Member Since 15 Oct 2012
Offline Last Active Dec 13 2012 01:29 AM

Topics I've Started

How do I insert a multimap into another multimap?

26 November 2012 - 03:32 PM

I've looked around and can't seem to find the answer.

Here is my multimap and an attempt at my idea of what I want to do:

[source lang="cpp"] // declared multimap of x/y/object std::multimap<int, std::multimap<int, GameObject*>*> gameObj; // pseodo code of what I want to do: GameObject* tempObj = new GameObject; gameObj.insert({ 1, { 1, tempObj });[/source]

Any ideas?

error: ‘filename’ was not declared in this scope

24 November 2012 - 10:30 AM

I seem to run into this issue where I try to use seperate file compilation and I end up getting the message:

error: ‘Component’ was not declared in this scope

I've declared the Component file, but I think this is a cyclic issue of some sort. Is there a way that I can avoid this problem?

Here are the files:

[source lang="cpp"]// main.cpp#include <iostream>#include <string>#include "gameobject.h"#include "component.h"int main (){ GameObject* go = new GameObject("Player"); Component* gun = new Component; go->AddComponent(gun);}[/source]

[source lang="cpp"]#ifndef COMPONENT_H#define COMPONENT_H#include <iostream>#include <string>#include "gameobject.h"class Component{public: std::string name; //The name of the component bool enabled; //Is the component currently enabled? GameObject* gameObject; //The game object that owns this component int type; //The type of component //Initializing Ctor Component(std::string Name) : name(Name), enabled(true) {} //Dtor virtual ~Component() {} //Abstract function, called when the scene is started virtual void OnStart() = 0; //Abstract function, called when the scene is updated virtual void OnUpdate() = 0;};#endif // COMPONENT_H[/source]

[source lang="cpp"]#ifndef GAMEOBJECT_H#define GAMEOBJECT_H#include <iostream>#include <vector>#include <string>#include "component.h"class GameObject{ // ERROR STARTS AT CODE BELOW (notice 'component.h' is declared above): std::vector<Component> components; //A list of components attached to the game objectpublic: std::string name; //The unique name of the game object //Transform transform; //The object's transform //Initializing Ctor GameObject(std::string Name = "Default Player") : name(Name) { } ~GameObject() { } //Finds the attached component with the name Name (if it exists)/* Component* GetComponent(std::string Name) { } //Finds the attached component with the index Idx (if it exists) Component* GetComponentAt(int Idx) { }*/ //Attaches a component to the game object void AddComponent(Component* NewComponent) { components.push_back(*NewComponent); } //Detaches the component with the name Name (if it exists) void RemoveComponent(std::string Name) { } //Detaches the component with the index Idx (if it exists) void RemoveComponentAt(int Idx) { } //Initializes the game object by initializing each of its attached components void Start() {} //Updates the game object by updating each of its attached components void Update() {}};#endif // GAMEOBJECT_H[/source]

Composition Based Game Outline. Would like input

20 November 2012 - 12:31 PM

I'm redesigning a 2D mario style side-scolling game from scratch. The code compiles with no warnings on -Wall and using C++11. I didn't include .cpp files as they were all blank and just include there header file.

Before I really get into this I would like some input on what I have so far. I'm wondering if I've got the right idea on how to implement composition:

[source lang="cpp"]//main.cpp#include <iostream>#include "character.h"int main(){ // create character object Character obj(Name(Title::goodguy), Point(1, 2)); // lets add body parts to build our character obj.getElement()->makeNew(eName::head); obj.getElement()->makeNew(eName::arms); obj.getElement()->makeNew(eName::waist); obj.getElement()->makeNew(eName::legs); // print out x position std::cout << "Point X: " << obj.getPoint()->getX() << std::endl; // now we'll move the character obj.getPoint()->setXY(10, 4); std::cout << "Point X: " << obj.getPoint()->getX() << std::endl; std::cout << "Dist moved: " << obj.getPoint()->getXDistMoved() << std::endl; std::cout << "Number of Elements (body parts): " << obj.getElement()->getNumElements() << std::endl; // leg was damaged in fight obj.getElement()->getElementName(eName::legs)->setHealth(40); // report on health of legs std::cout << "Health of legs: " << obj.getElement()->getElementName(eName::legs)->getHealth() << std::endl; // delete the arms because they were shot off obj.getElement()->deleteElementName(eName::arms); // how many elements do we have now? std::cout << "Number of Elements: " << obj.getElement()->getNumElements() << std::endl; std::cout << "" << std::endl;}[/source]

[source lang="cpp"]// character.h#ifndef CHARACTER_H#define CHARACTER_H#include "name.h"#include "number.h"#include "point.h"#include "element.h"class Character{private: Name name; Number ID; Number player; Point point; Point platformPoint; Element element;public: Character() {} Character(const Name& name, const Point& point) : name(name), point(point) {} Character(const Name& name, const Point& pointIn, const Element& element) : name(name), point(point), element(element) {} // characters features Name* getName() { return &name; } Number* getID() { return &ID; } Number* getPlayerNum() { return &player; } Point* getPoint() { return &point; } Point* getPlatformPoint() { return &platformPoint; } Element* getElement() { return &element; } };#endif[/source]
[source lang="cpp"]//element.h#ifndef ELEMENT_H#define ELEMENT_H#include <map>#include "properties.h"enum class eName // element name{ none, // good & bad character parts head, leftArm, rightArm, arms, waist, legs, leftLeg, rightLeg, // board & scenery top, bottom, left, right};class Element{private: std::multimap<eName, Properties> vElement; // map array to store elementspublic: Element() {} Element(const eName& name) { Properties properties; vElement.insert({ name, properties }); } Element(const eName& name, Properties& properties) { vElement.insert({ name, properties }); } void makeNew(const eName& name) { Properties properties; vElement.insert({ name, properties }); } void makeNew(const eName& name, Properties& properties) { vElement.insert({ name, properties }); } unsigned int getNumElements() const { return vElement.size(); } // search Properties* getElementName(eName name) { std::multimap<eName, Properties>::iterator it; it = vElement.find(name); // make sure element exists if(it != vElement.end()) return &it->second; // element name doesn't exist so make a new one Properties *properties = new Properties; vElement.insert({ name, (*properties) }); return properties; } // delete void deleteElementName(eName name) { vElement.erase(name); } };#endif[/source]
[source lang="cpp"]// name.h#ifndef NAME_H#define NAME_Henum class Title{ goodguy, badguy, scenery, item, weapon, custom, null};class Name{private: Title title;public: Name(const Title& name = Title::null) : title(name) {}; ~Name() {} };#endif[/source]
[source lang="cpp"]// properties.h#ifndef PROPERTIES_H#define PROPERTIES_H#include "point.h"enum class viscosity { normal, slippery, sticky};enum class solidity { solid, upPass, downPass, rightPass, leftPass, invisible, semi };enum class visibility { visible, invisible, semiVisisble };class Properties{private: Point relativePoint; // xy point relative to general xy position unsigned int health; public: // constructor/destructor Properties() : health(100) {}; ~Properties() {} void setHealth(const unsigned int& i) { health = i; } unsigned int getHealth() { return health; } };#endif[/source]
[source lang="cpp"]// point.h#ifndef POINT_H#define POINT_Hclass Point{private: int x; int y; int lastX; int lastY;public: // constructor/destructor Point(const int& x = 0, const int& y = 0) : x(x), y(y), lastX(0), lastY(0) {}; ~Point() {} // set/get individually void setX(const int& i) { lastX = x; x = i; } int getX() const { return x; } void setY(const int& i) { lastY = y; y = i; } int getY() const { return y; } // set/get as a pair void setXY(const int& x, const int& y){ lastX = this->x; lastY = this->y; this->x = x; this->y = y; } // set/get using an object void setTo(Point& point){ lastX = x; lastY = y; x = point.getX(); y = point.getY(); } // compare x/y difference to another object int getCompareX(Point& point) { return point.getX() - this->x; } int getCompareY(Point& point) { return point.getY() - this->y; } // calculate distance moved int getXDistMoved() { return this->x - lastX; } int getYDistMoved() { return this->y - lastY; } // @TODO - record function records all moves };#endif[/source]

[source lang="cpp"]// number.h#ifndef NUMBER_H#define NUMBER_Hclass Number{private: int num;public: // constructor/destructor Number(const int& num = 0) : num(num) {} // set/get individually void setNum(const int& num) { this->num = num; } int getNum() const { return num; } };#endif[/source]

General Class Structure Question

19 November 2012 - 02:15 PM

I've made a 2D mario style side-scroller. It works fine. My issue is that I don't think I'm using inheritance and polymorphism to the best of my ability. The result is that to use one class method I need to bring over a whole bunch of parameters from other class methods and containers.

For example:

instead of just drawing the board this way:

draw(imageToDraw, x, y);

I end up having to do this:

draw(game, draw, objects, x, y);

This wouldn't be an issue if it wasn't happening on so many methods of different functions and makes the code so much more complex looking and cumbersome. Another issue that makes it a little more challenging is that this game allows for multiple games at once. Basically split screen for an infinite amount of games. I do not want to drop this functionality.

So here are my basic classes:

Game - main loop
Draw - draws objects
Camera - tells Draw the location of objects to draw
Object - stores object xy velocity, characteristics...
Enemy Player Board - sets object characterists
Factory - object factory creates games objects
Move - moves objects
Collision - checks for crash
Record - records objects movement and plays back
Keyboard - game input

Here is my idea and I'd like some input on it. What do you think of combination like this ( where '->' means derived from):

game ->
camera -> draw
factory -> object -> enemy/player/board
move -> record -> collision -> keyboard

This is basically how I have it except I'm not inheriting from 'game' so I'm a lot of passing parameters. Would this be the better option and just inherit from 'game'?:

object -> enemy/player/board

I could use some ideas. I ask this because reworking this will take days. If I can get some ideas perhaps this will make things easier in the future.

Storing an object in a struct

26 October 2012 - 10:06 AM

I would like to store an object in a struct. This is what I have so far:

struct sectionData
unsigned int currentSectionNumber;
unsigned int currentObjNum;
unsigned int sectionNumberToGoIn;
int camFocus;
Object obj;  // this is the object i want to store in the struct. how to do it?

// store data too struct
sectionData sectData;

// pushback container

// apply info
sectData.currentSectionNumber = 1;
sectData.currentObjNum = 20;
sectData.sectionNumberToGoIn = 0;
sectData.camfocus = 1;
sectData.Object = gameObject; // ?? not sure what to do here

// retrieve info
how to retrieve Object??

Any suggestion?