Meteor2D  dev internal
Meteor is a lightweight 2D game engine.
Loading...
Searching...
No Matches
spatial.h
1#pragma once
2#include <iostream>
3#include<vector>
4#include <meteorutils/typehelpers.h>
5#include<meteorutils/vector2d.h>
6#include<entities/entity.h>
7#include <SDL.h>
8
9namespace meteor {
15 class MSpatialEntity : public MEntity {
16 public:
20 inline MSpatialEntity() : MSpatialEntity(NULL) {};
27
31 inline SVector2 getPosition() { return position; }
35 inline SVector2 getLocalPosition() { return localPosition; }
41 inline void setLocalPosition(float x, float y) { localPosition.x = x; localPosition.y = y; updatePositions(); }
46 inline void setLocalPosition(SVector2 pos) { localPosition.x = pos.x; localPosition.y = pos.y; updatePositions(); }
47
51 inline float getRotation() { return rotation; }
55 inline float getLocalRotation() { return localRotation; }
60 inline void setLocalRotation(float localRotation) { this->localRotation = localRotation; }
64 SVector2 getLocalScale() { return localScale; };
70 inline void setLocalScale(float x, float y) { localScale.x = x; localScale.y = y; }
75 inline void setLocalScale(SVector2 scale) { localScale.x = scale.x; localScale.y = scale.y; }
76
80 inline MSpatialEntity* getParent() { return parent; }
84 inline std::vector<MSpatialEntity*>* getChildren() { return children; }
85
90 void addChild(MSpatialEntity* entity);
96
103 template<typename T>
104 T* find(std::string name) {
105
106 // is this node called 'name'
107 if (this->name == name)
108 return (T*)this;
109
110 // if no more children return NULL
111 if (children->size() <= 0)
112 return NULL;
113
114 // check recursively for each child
115 for (auto child : *children) {
116 if (child == NULL)
117 continue;
118
119 auto res = child->find<T>(name);
120 if (res == NULL)
121 continue;
122
123 auto isValidInstance = instanceof<T>((T*)res);
124 //if we found it, return
125 if (res != NULL && isValidInstance)
126 return (T*)res;
127 }
128
129 // if everything fails, return NULL
130 return NULL;
131 }
132
133 virtual void onStart();
134 virtual void onUpdate(float deltaTime);
135 virtual void onExit();
136
137 protected:
138 SVector2 localPosition;
139 SVector2 localScale;
140 SVector2 position;
141 float rotation;
142 float localRotation;
143 MSpatialEntity* parent = NULL;
144 std::vector<MSpatialEntity*>* children = NULL;
145
146 void updatePositions();
147 void updateRotations();
148 void updateScale();
149
150 void updateChildren(float deltaTime);
151 private:
152 void setParent(MSpatialEntity* entity) { parent = entity; };
153 };
154}
An Entity is the base class for all data and behaviour oriented objects in meteor....
Definition entity.h:9
Spatial Entity is the base entity for all types present in scene. This entity keeps track of spatial ...
Definition spatial.h:15
void setLocalScale(SVector2 scale)
brief Sets local scale
Definition spatial.h:75
void setLocalPosition(SVector2 pos)
Sets local position of the entity.
Definition spatial.h:46
SVector2 getLocalPosition()
Definition spatial.h:35
void removeChild(MSpatialEntity *entity)
Remove a child from this entity.
std::vector< MSpatialEntity * > * getChildren()
Definition spatial.h:84
void setLocalScale(float x, float y)
Sets local scale.
Definition spatial.h:70
float getRotation()
Definition spatial.h:51
MSpatialEntity()
Default constructor, instantiates at root level.
Definition spatial.h:20
SVector2 getLocalScale()
Definition spatial.h:64
void addChild(MSpatialEntity *entity)
Adds a child to this entity.
SVector2 getPosition()
Definition spatial.h:31
MSpatialEntity(MSpatialEntity *parent)
Parameterised constructor.
float getLocalRotation()
Definition spatial.h:55
MSpatialEntity * getParent()
Definition spatial.h:80
void setLocalPosition(float x, float y)
Sets local position of the entity.
Definition spatial.h:41
void setLocalRotation(float localRotation)
Sets local rotation of the entity in x-axis.
Definition spatial.h:60
T * find(std::string name)
Recursively searches for a SpatialEntity within this sub-tree.
Definition spatial.h:104
A structure representing a 2D Vector.
Definition vector2d.h:11