Guest User

Untitled

a guest
Jul 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.37 KB | None | 0 0
  1. //////////World.h//////////
  2.  
  3. #ifndef WORLD_H_INCLUDED
  4. #define WORLD_H_INCLUDED
  5.  
  6. #include <iostream>
  7. #include <vector>
  8.  
  9. #include "Ogre.h"
  10. #include "Object.h"
  11. #include "PointLight.h"
  12. #include "StaticMesh.h"
  13.  
  14. class World {
  15.     public:
  16.         World();
  17.         ~World();
  18.  
  19.         std::map<int, Object*> objects;
  20.         int nextObjectId;
  21.         Ogre::SceneNode *worldNode;
  22.  
  23.         void setup();
  24.         void simulate();
  25.         void end();
  26.         void addCamera();
  27.         void setAmbientLight();
  28.         void setAmbientLight(float r, float b, float g);
  29.  
  30.         template<typename ObjectType> ObjectType* spawn() {
  31.             ObjectType* result = new ObjectType();
  32.  
  33.             objects[nextObjectId++] = result;
  34.  
  35.             result->node = worldNode->createChildSceneNode(result->newNamedId("onode"));
  36.         }
  37.  
  38. };
  39.  
  40. #endif // WORLD_H_INCLUDED
  41.  
  42. //////////World.cpp//////////
  43.  
  44. #include "World.h"
  45. #include "Engine.h"
  46.  
  47. World::World() : nextObjectId(-1) {
  48.     std::cout << "World has been created" << std::endl;
  49.  
  50.     // create the worlds root node
  51.     worldNode = Engine::getSingleton()->manager->createSceneNode("wnode");
  52.  
  53.     setAmbientLight();
  54. }
  55.  
  56. World::~World() {
  57. }
  58.  
  59. void World::simulate() {
  60.     for(std::map<int, Object*>::iterator i = objects.begin(); i != objects.end(); ++i) {
  61.        (*i).second->update();
  62.     }
  63. }
  64.  
  65. void World::end() {
  66.     std::cout << "world has ended" << std::endl;
  67. }
  68.  
  69. void World::setAmbientLight() {
  70.     Engine::getSingleton()->manager->setAmbientLight(Ogre::ColourValue(1.0f, 1.0f, 1.0f));
  71. }
  72.  
  73. void World::setAmbientLight(float r, float b, float g) {
  74.     Engine::getSingleton()->manager->setAmbientLight(Ogre::ColourValue(r, b, g));
  75. }
  76.  
  77. //////////Object.h//////////
  78.  
  79. #ifndef OBJECT_H_INCLUDED
  80. #define OBJECT_H_INCLUDED
  81.  
  82. #include <iostream>
  83. #include <vector>
  84.  
  85. #include "Component.h"
  86. #include "MeshComponent.h"
  87. #include "Modifier.h"
  88. #include "Ogre.h"
  89.  
  90. typedef enum {
  91.     ALIVE,    // object is being updates as normal
  92.     INACTIVE, // object still exists but isnt visible or being updated
  93.     PAUSED,   // object is stopped in its tracks and will resume updating when state returns to ACTIVE
  94.     DEAD      // object is not visible, updated or interactive, it simply exists in memory and will be deleted on the next update pass
  95. } ObjectState;
  96.  
  97. class Object {
  98.     public:
  99.         Object();
  100.         ~Object();
  101.         Ogre::Vector3 location;
  102.         Ogre::String name;
  103.         Ogre::String prefix;
  104.         int id;
  105.         ObjectState state;
  106.         float delta;
  107.         Ogre::SceneNode *node;
  108.         bool track();
  109.         virtual void update(float delta = 1.0f);
  110.  
  111.         // easily add a component to an object. Accepts a component pointer
  112.         // returns the component id
  113.         template <class C> void addComponent(C *c) {
  114.             if(c->entity != NULL) {
  115.                 c->node = node->createChildSceneNode(newNamedId("cnode"));
  116.                 c->node->attachObject(c->entity);
  117.             }
  118.  
  119.             components.push_back(c);
  120.         }
  121.  
  122.         template <class C> void dropComponent(C *c) {
  123.         }
  124.  
  125.         Ogre::String newNamedId(Ogre::String str);
  126.  
  127.         void updateComponents();
  128.         void addModifier();
  129.         void dropModifier();
  130.         void updateModifiers();
  131.         void destroy();
  132.         bool requestStateChange(ObjectState newState);
  133.         void lockState();
  134.         void unlockState();
  135.         float getDelta();
  136.         float _D(float d);
  137.  
  138.     private:
  139.         // a vector to store components
  140.         std::vector<Component*> components;
  141.         // a vector to store modifiers
  142.         std::vector<Modifier*> modifiers;
  143. };
  144.  
  145. #endif // OBJECT_H_INCLUDED
  146.  
  147. //////////Object.cpp//////////
  148.  
  149. #include "Object.h"
  150. #include "Engine.h"
  151.  
  152. Object::Object() {
  153.     std::cout << "An object has been created" << std::endl;
  154. }
  155.  
  156. Object::~Object() {
  157.     Engine::getSingleton()->world->objects.erase(id);
  158.  
  159.     std::cout << "An object has been destroyed" << std::endl;
  160. }
  161.  
  162. Ogre::String Object::newNamedId(Ogre::String str)
  163. {
  164.     return str + Ogre::StringConverter::toString(Engine::getSingleton()->world->nextObjectId++);
  165. }
  166.  
  167. // core update function
  168. // this is the default update function.
  169. // when the user wants to add their own update logic, they should pass float deltaTime as the function parameter
  170. // the default update function doesnt need any access to delta time, but it does ensure that deltaTime is constantly kept up to date
  171. // it also handles automatic updating for object modifiers and components
  172. bool Object::track() {
  173.     if(state == DEAD)
  174.     {
  175.         return false;
  176.     }
  177.  
  178.     update(delta);
  179.  
  180.     updateComponents();
  181.  
  182.     return true;
  183. }
  184.  
  185. // user update function
  186. // 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
  187. void Object::update(float deltaTime) {
  188.     //std::cout << "an object is being updated" << std::endl;
  189.  
  190.     //node->translate(Ogre::Vector3(location.x, location.y, location.z + 0.01));
  191. }
  192.  
  193. // handles automatic update of all modifiers added to an object instance
  194. void Object::updateModifiers() {
  195.     for(int i = 0; i < modifiers.size(); ++i) {
  196.         modifiers[i]->update();
  197.     }
  198. }
  199.  
  200. // handles automatic update of all components added to an object instance
  201. void Object::updateComponents() {
  202.     for(int i = 0; i < components.size(); ++i) {
  203.         components[i]->update();
  204.     }
  205. }
  206.  
  207. // easily add a modifier to an object. Accepts a modifier pointer
  208. // returns the modifier id
  209. void Object::addModifier() {
  210. }
  211.  
  212. // drop a modifier from the objects list of components
  213. // accepts a modifier id(int)
  214. void Object::dropModifier() {
  215. }
  216.  
  217. // 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
  218. void Object::destroy() {
  219.     if(requestStateChange(DEAD)) {
  220.         lockState();
  221.     }
  222. }
  223.  
  224. void deleteObject() {
  225. }
  226.  
  227. // change the state of the object
  228. // first checks if the state change is valid. if it is, change the state and return true, else just return false
  229. bool Object::requestStateChange(ObjectState newState) {
  230.     state = newState;
  231.  
  232.     return true;
  233. }
  234.  
  235. // make the state unchangeable
  236. void Object::lockState() {
  237. }
  238.  
  239. // make the state changeable again
  240. void Object::unlockState() {
  241. }
  242.  
  243. // get delta time
  244. float Object::getDelta() {
  245.     delta = 1.0f;
  246.  
  247.     return delta;
  248. }
  249.  
  250. // calculate new value for a value which relies on deltatime
  251. float Object::_D(float d) {
  252.     return d;
  253. }
  254.  
  255. //////////Component.h//////////
  256.  
  257. #ifndef COMPONENT_H_INCLUDED
  258. #define COMPONENT_H_INCLUDED
  259.  
  260. #include "Ogre.h"
  261.  
  262. class Object;
  263.  
  264. class Component {
  265.     public:
  266.         Component();
  267.         ~Component();
  268.  
  269.         Object *owner;
  270.         Ogre::Entity * entity;
  271.         Ogre::Vector3 location;
  272.         Ogre::SceneNode *node;
  273.  
  274.         void clean();
  275.         virtual void init();
  276.         virtual void update();
  277. };
  278.  
  279. #endif // COMPONENT_H_INCLUDED
  280.  
  281. //////////Component.cpp//////////
  282.  
  283. #include "Component.h"
  284. #include "Engine.h"
  285.  
  286. Component::Component() : node(NULL), entity(NULL){
  287. }
  288.  
  289. Component::~Component() {
  290. }
  291.  
  292. void Component::clean() {
  293.     Engine::getSingleton()->manager->destroyEntity(entity);
  294.     Engine::getSingleton()->manager->destroySceneNode(node);
  295. }
  296.  
  297. void Component::init() {
  298. }
  299.  
  300. void Component::update() {
  301. }
Add Comment
Please, Sign In to add comment