]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Started some fancy work on components
authorAndy <drumsetmonkey@gmail.com>
Fri, 14 Oct 2016 12:19:30 +0000 (08:19 -0400)
committerAndy <drumsetmonkey@gmail.com>
Fri, 14 Oct 2016 12:19:30 +0000 (08:19 -0400)
brice.dat
include/components.hpp [new file with mode: 0644]
src/entities.cpp
xml/!town.xml
xml/mobs.xml [new file with mode: 0644]

index 2653f9ce5cd4f99cef1fe2519168885e87cffb42..308f5c8c2c968edc4e514e70d9d83d86dfdce438 100644 (file)
--- a/brice.dat
+++ b/brice.dat
@@ -1,7 +1,7 @@
 3
 Slow
-1
-canJump
 0
+canJump
+1
 canSprint
-0
+1
diff --git a/include/components.hpp b/include/components.hpp
new file mode 100644 (file)
index 0000000..34a1906
--- /dev/null
@@ -0,0 +1,137 @@
+/**
+ * @file components.hpp
+ * @brief Where all of an enities possible components are stored.
+ * Using an ECS (Entity component system) the entities are given components on the fly,
+ * this allows the entity to change stats or skills on the go. This also allows every "object"
+ * the be an entity, and it gives the game a much better customizability over xml.
+ */
+
+#ifndef COMPONENTS_HPP
+#define COMPONENTS_HPP
+
+#include <entityx/entityx.h>
+
+/**
+ * @struct Position
+ * @brief Stores the position of an entity on the xy plane.
+ */
+struct Position {
+       /**
+        * Constructor that sets the position of the object, if nothing is passed it will default to 0.
+        * @param x The x position the object will be placed at.
+        * @param y the y position the object will be placed at.
+        */
+       Position(float x = 0.0f, float y = 0.0f): x(x), y(y) {}
+
+       float x; /**< The x position in the world */
+       float y; /**< The y position in the world */
+};
+
+/**
+ * @struct Direction
+ * @brief Store an entities velocity.
+ * This allows the entity to move throughout the world.
+ */
+struct Direction {
+       /**
+        * Constructor that sets the velocity, if no position is passed, it defaults to (0,0).
+        * @param x The velocity of the object on the x axis.
+        * @param y The velocity of the object on the y axis.
+        */
+       Direction(float x = 0.0f, float y = 0.0f): x(x), y(y) {}
+
+       float x; /**< Velocity the object is moving in the x direction, this is added to the position */
+       float y; /**< Velocity the object is moving in the y direction, this is added to the position */
+};
+
+/**
+ * @struct Physics
+ * @brief Allows and entity to react to gravity and frictions.
+ * When an entity inherits this component it will react with gravity and move with friction.
+ */
+struct Physics {
+       /**
+        * Constructor that sets the gravity constant, if not specified it becomes 0.
+        * @param g The non default gravity constant.
+        */
+       Physics(float g = 0.0f): g(g) {}
+
+       float g; /**< The gravity constant, how fast the object falls */
+};
+
+/**
+ * @struct Solid
+ * @brief Allows an entity to collide with other objects.
+ * When an entity has this component it can collide with the world and other objects.
+ */
+struct Solid {
+       /**
+        * Constructor that sets the entities dimensions based on what is passed.
+        * @param w The desired width of the entity.
+        * @param h The desired height of the entity.
+        */
+       Solid(float w = 0.0f, float h = 0.0f): width(w), height(h) {}
+
+       float width; /**< The width of the entity in units */
+       float height; /**< The height of the entity in units */
+};
+
+struct SpriteData {
+       uint sheetID;
+       vec2 offset;
+       vec2 size;
+};
+
+//TODO
+/**
+ * @struct Sprite
+ * @brief If an entity is visible we want to be able to see it.
+ * Each entity is given a sprite, a sprite can consist of manu frames or pieces to make one.
+ */
+struct Sprite {
+       std::vector<std::pair<SpriteData, vec2>> getSprite() {
+               return sprite;
+       }
+       
+       int clearSprite() {
+               if (sprite.empty())
+                       return 0;
+
+               sprite.clear();
+               return 1;       
+       }
+       
+       int addSpriteSegment(SpriteData data, vec2 loc) {
+               //TODO if sprite is in this spot, do something
+               sprite.push_back(std::make_pair(data, loc));
+               return 1;
+       }
+
+       int changeSpriteSegment(SpriteData data, vec2 loc) {
+               for (auto &s : sprite) {
+                       if (s.second == loc) {
+                               s.first = data;
+                               
+                               return 1;
+                       }
+               }
+               addSpriteSegment(data, loc);
+               return 0;
+       }
+
+       std::vector<std::pair<SpriteData, vec2>> sprite;
+};
+
+//TODO
+struct Animate {
+       std::vector<std::pair<SpriteData, vec2>> sprite_e;
+       std::vector<std::pair<SpriteData, vec2>> sprite_c;
+};
+
+//TODO
+
+struct Input {
+       
+};
+
+#endif //COMPONENTS_HPP
index 0f8c3b71b457b1e57b5b271d406e730832f02a9e..3afcc40a577de2f803e5f0e647ccc2fc4b9b1a77 100644 (file)
 #include <render.hpp>
 #include <engine.hpp>
 
+///NEW
+#include <components.hpp>
+#include <entityx/entityx.h>
+///OLD
+
 extern std::istream *names;
 
 extern Player *player;                 // main.cpp
index a03e0bf71af9427601d21a7444509e5f09ac3644..3ac977282d44157941baa8f917f1b98137aacaed 100644 (file)
@@ -4,11 +4,11 @@
     <generation type="Random" width="1600"/>
     <time>6000</time>
     <spawnx>-300</spawnx>
-    <npc name="Sanc" hasDialog="true"/>
-    <npc name="Bob" hasDialog="true" spawnx="30"/>
-    <structure type="1" spawnx="300"/>
-    <structure inside="bobshouse.xml" type="1" spawnx="10"/>
-    <chest/>
+    <npc name="Sanc" hasDialog="true" health="1" x="64.841042" y="68.19912" dindex="0"/>
+    <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="94.841454" y="68.79911" dindex="0"/>
+    <structure type="1" spawnx="300" alive="1"/>
+    <structure inside="bobshouse.xml" type="1" spawnx="10" alive="1"/>
+    <chest alive="1"/>
 </World>
 
 <Dialog name="Bob">
diff --git a/xml/mobs.xml b/xml/mobs.xml
new file mode 100644 (file)
index 0000000..238c582
--- /dev/null
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<mob name="rabbit">
+       <components>
+               <position/>
+               <direction/>
+               <physics/>
+               <solid/>
+               <sprite/>
+               <animation/>
+               <visible/>
+               <health>10</health>
+               <ai/>
+               <wander/>
+       </components>
+</mob>