kick
/Users/morten/Programmering/cpp/kick/src/kick/scene/scene.h
00001 //
00002 //  Scene.h
00003 //  KickCPP
00004 //
00005 //  Created by Morten Nobel-Jørgensen on 5/21/13.
00006 //  Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved.
00007 //
00008 
00009 #pragma once
00010 
00011 #include <iostream>
00012 #include <vector>
00013 #include <string>
00014 #include <memory>
00015 #include <unordered_map>
00016 #include <utility>
00017 #include <functional>
00018 #include <map>
00019 #include "kick/scene/game_object.h"
00020 #include "kick/scene/component.h"
00021 #include "kick/core/event.h"
00022 #include "kick/scene/scene_lights.h"
00023 #include "kick/scene/camera_perspective.h"
00024 #include "kick/scene/line_renderer.h"
00025 #include "kick/scene/camera_orthographic.h"
00026 #include "kick/2d/label.h"
00027 
00028 namespace kick {
00029     class CameraPerspective;
00030     class CameraOrthographic;
00031     class MeshRenderer;
00032     class Light;
00033     class Updatable;
00034     class Label;
00035     class Sprite;
00036     class TextureAtlas;
00037     class Button;
00038     class Canvas;
00039 
00040     class Scene {
00041     public:
00042         Scene(Scene&& scene);
00043         Scene& operator=(Scene&& other);
00044         virtual ~Scene();
00045         GameObject *createGameObject(const std::string &name = "gameobject");
00046         void destroyGameObject(GameObject * gameObject);
00047         GameObjectIter begin() const;
00048         GameObjectIter end() const;
00049         std::string name() const;
00050         void setName(std::string name);
00051         void update();
00052         void render(EngineUniforms* engineUniforms);
00053         Event<std::pair<std::shared_ptr<Component>, ComponentUpdateStatus>> componentEvents;
00054 
00055         template <typename T>
00056         std::vector<std::shared_ptr<T>> findComponents(){
00057             std::vector<std::shared_ptr<T>> res;
00058             for (auto & gameObject : *this){
00059                 for (auto & comp : *gameObject){
00060                     auto component = std::dynamic_pointer_cast<T>(comp);
00061                     if (component){
00062                         res.push_back(component);
00063                     }
00064                 }
00065             }
00066             return res;
00067         }
00068 
00069         template <typename T>
00070         std::shared_ptr<T> findComponent(){
00071             for (auto & gameObject : *this){
00072                 for (auto & comp : *gameObject){
00073                     auto component = std::dynamic_pointer_cast<T>(comp);
00074                     if (component){
00075                         return component;
00076                     }
00077                 }
00078             }
00079             return nullptr;
00080         }
00081 
00082         // Creates a gameobject and attaches a perspective camera to it
00083         std::shared_ptr<CameraPerspective> createPerspectiveCamera(GameObject *go = nullptr);
00084         // Creates a gameobject and attaches an orthographic camera to it
00085         std::shared_ptr<CameraOrthographic> createOrthographicCamera(GameObject *go = nullptr);
00086         // Creates a gameobject and attaches a line renderer with diffuse renderer
00087         std::shared_ptr<LineRenderer> createLine(GameObject *go = nullptr, const std::vector<glm::vec3> &points = std::vector<glm::vec3>(), MeshType meshType = MeshType::Lines, const std::vector<GLushort> &indices = std::vector<GLushort>{});
00088         // Creates a gameobject and attaches a cube with diffuse renderer
00089         std::shared_ptr<MeshRenderer> createCube(GameObject *go = nullptr, float length = 1);
00090         // Creates a gameobject and attaches a sphere with diffuse renderer
00091         std::shared_ptr<MeshRenderer> createSphere(GameObject *go = nullptr);
00092         // Creates a gameobject and attaches a plane with diffuse renderer
00093         std::shared_ptr<MeshRenderer> createPlane(GameObject *go = nullptr);
00094         // Creates a gameobject and attaches a point light
00095         std::shared_ptr<Light> createPointLight(GameObject *go = nullptr);
00096         // Creates a gameobject and attaches a directional light
00097         std::shared_ptr<Light> createDirectionalLight(GameObject *go = nullptr);
00098         // Creates a gameobject and attaches an ambient light
00099         std::shared_ptr<Light> createAmbientLight(float intensity = 0.3f, glm::vec3 color = glm::vec3(1));
00100         // create a Canvas
00101         std::shared_ptr<Canvas> createCanvas(bool includeUICamera = true);
00102 
00103         friend class Engine;
00104         friend class GameObject;
00105 
00106         GameObject *gameObjectByUID(int32_t uid);
00107 
00108         // Return the first camera component marked as main camera (or any camera if no camera marked).
00109         // Return nullptr if no camera component in scene
00110         std::shared_ptr<Camera> mainCamera();
00111     private:
00112         Scene(const std::string & name);
00113         Scene(const Scene& scene) = delete;
00114         void componentListener(std::shared_ptr<Component> component, ComponentUpdateStatus status);
00115         void addLight(std::shared_ptr<Light> light);
00116         std::vector<std::unique_ptr<GameObject>> mGameObjects;
00117         std::map<GameObject*,EventListener<std::pair<std::shared_ptr<Component>, ComponentUpdateStatus>>> mComponentListeners;
00118         std::vector<std::shared_ptr<Camera>> mCameras;
00119         std::vector<std::shared_ptr<Updatable>> mUpdatable;
00120         std::unordered_map<std::shared_ptr<Light>, EventListener<std::shared_ptr<Light>>> mLights;
00121         SceneLights mSceneLights;
00122         std::string mName = "";
00123 
00124         int32_t mUniqueIdGenerator = 0;
00125 
00126         void rebuildSceneLights();
00127     };
00128 };
00129 
00130 
 All Classes Functions Variables