Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////World.h//////////
- #ifndef WORLD_H_INCLUDED
- #define WORLD_H_INCLUDED
- #include <iostream>
- #include <vector>
- #include "Ogre.h"
- #include "Object.h"
- #include "PointLight.h"
- #include "StaticMesh.h"
- class World {
- public:
- World();
- ~World();
- std::map<int, Object*> objects;
- int nextObjectId;
- Ogre::SceneNode *worldNode;
- void setup();
- void simulate();
- void end();
- void addCamera();
- void setAmbientLight();
- void setAmbientLight(float r, float b, float g);
- template<typename ObjectType> ObjectType* spawn() {
- ObjectType* result = new ObjectType();
- objects[nextObjectId++] = result;
- result->node = worldNode->createChildSceneNode(result->newNamedId("onode"));
- }
- };
- #endif // WORLD_H_INCLUDED
- //////////World.cpp//////////
- #include "World.h"
- #include "Engine.h"
- World::World() : nextObjectId(-1) {
- std::cout << "World has been created" << std::endl;
- // create the worlds root node
- worldNode = Engine::getSingleton()->manager->createSceneNode("wnode");
- setAmbientLight();
- }
- World::~World() {
- }
- void World::simulate() {
- for(std::map<int, Object*>::iterator i = objects.begin(); i != objects.end(); ++i) {
- (*i).second->update();
- }
- }
- void World::end() {
- std::cout << "world has ended" << std::endl;
- }
- void World::setAmbientLight() {
- Engine::getSingleton()->manager->setAmbientLight(Ogre::ColourValue(1.0f, 1.0f, 1.0f));
- }
- void World::setAmbientLight(float r, float b, float g) {
- Engine::getSingleton()->manager->setAmbientLight(Ogre::ColourValue(r, b, g));
- }
- //////////Object.h//////////
- #ifndef OBJECT_H_INCLUDED
- #define OBJECT_H_INCLUDED
- #include <iostream>
- #include <vector>
- #include "Component.h"
- #include "MeshComponent.h"
- #include "Modifier.h"
- #include "Ogre.h"
- typedef enum {
- ALIVE, // object is being updates as normal
- INACTIVE, // object still exists but isnt visible or being updated
- PAUSED, // object is stopped in its tracks and will resume updating when state returns to ACTIVE
- DEAD // object is not visible, updated or interactive, it simply exists in memory and will be deleted on the next update pass
- } ObjectState;
- class Object {
- public:
- Object();
- ~Object();
- Ogre::Vector3 location;
- Ogre::String name;
- Ogre::String prefix;
- int id;
- ObjectState state;
- float delta;
- Ogre::SceneNode *node;
- bool track();
- virtual void update(float delta = 1.0f);
- // easily add a component to an object. Accepts a component pointer
- // returns the component id
- template <class C> void addComponent(C *c) {
- if(c->entity != NULL) {
- c->node = node->createChildSceneNode(newNamedId("cnode"));
- c->node->attachObject(c->entity);
- }
- components.push_back(c);
- }
- template <class C> void dropComponent(C *c) {
- }
- Ogre::String newNamedId(Ogre::String str);
- void updateComponents();
- void addModifier();
- void dropModifier();
- void updateModifiers();
- void destroy();
- bool requestStateChange(ObjectState newState);
- void lockState();
- void unlockState();
- float getDelta();
- float _D(float d);
- private:
- // a vector to store components
- std::vector<Component*> components;
- // a vector to store modifiers
- std::vector<Modifier*> modifiers;
- };
- #endif // OBJECT_H_INCLUDED
- //////////Object.cpp//////////
- #include "Object.h"
- #include "Engine.h"
- Object::Object() {
- std::cout << "An object has been created" << std::endl;
- }
- Object::~Object() {
- Engine::getSingleton()->world->objects.erase(id);
- std::cout << "An object has been destroyed" << std::endl;
- }
- Ogre::String Object::newNamedId(Ogre::String str)
- {
- return str + Ogre::StringConverter::toString(Engine::getSingleton()->world->nextObjectId++);
- }
- // core update function
- // this is the default update function.
- // when the user wants to add their own update logic, they should pass float deltaTime as the function parameter
- // the default update function doesnt need any access to delta time, but it does ensure that deltaTime is constantly kept up to date
- // it also handles automatic updating for object modifiers and components
- bool Object::track() {
- if(state == DEAD)
- {
- return false;
- }
- update(delta);
- updateComponents();
- return true;
- }
- // user update function
- // by passing the deltatime as an argument it allows the end user to utilise the overloaded update function where they can add their own update logic
- void Object::update(float deltaTime) {
- //std::cout << "an object is being updated" << std::endl;
- //node->translate(Ogre::Vector3(location.x, location.y, location.z + 0.01));
- }
- // handles automatic update of all modifiers added to an object instance
- void Object::updateModifiers() {
- for(int i = 0; i < modifiers.size(); ++i) {
- modifiers[i]->update();
- }
- }
- // handles automatic update of all components added to an object instance
- void Object::updateComponents() {
- for(int i = 0; i < components.size(); ++i) {
- components[i]->update();
- }
- }
- // easily add a modifier to an object. Accepts a modifier pointer
- // returns the modifier id
- void Object::addModifier() {
- }
- // drop a modifier from the objects list of components
- // accepts a modifier id(int)
- void Object::dropModifier() {
- }
- // destroy the instance. although the instance isnt directly destroyed, it's state is set to dead and the object manager will clear out any dead objects on its next update pass
- void Object::destroy() {
- if(requestStateChange(DEAD)) {
- lockState();
- }
- }
- void deleteObject() {
- }
- // change the state of the object
- // first checks if the state change is valid. if it is, change the state and return true, else just return false
- bool Object::requestStateChange(ObjectState newState) {
- state = newState;
- return true;
- }
- // make the state unchangeable
- void Object::lockState() {
- }
- // make the state changeable again
- void Object::unlockState() {
- }
- // get delta time
- float Object::getDelta() {
- delta = 1.0f;
- return delta;
- }
- // calculate new value for a value which relies on deltatime
- float Object::_D(float d) {
- return d;
- }
- //////////Component.h//////////
- #ifndef COMPONENT_H_INCLUDED
- #define COMPONENT_H_INCLUDED
- #include "Ogre.h"
- class Object;
- class Component {
- public:
- Component();
- ~Component();
- Object *owner;
- Ogre::Entity * entity;
- Ogre::Vector3 location;
- Ogre::SceneNode *node;
- void clean();
- virtual void init();
- virtual void update();
- };
- #endif // COMPONENT_H_INCLUDED
- //////////Component.cpp//////////
- #include "Component.h"
- #include "Engine.h"
- Component::Component() : node(NULL), entity(NULL){
- }
- Component::~Component() {
- }
- void Component::clean() {
- Engine::getSingleton()->manager->destroyEntity(entity);
- Engine::getSingleton()->manager->destroySceneNode(node);
- }
- void Component::init() {
- }
- void Component::update() {
- }
Add Comment
Please, Sign In to add comment