Bläddra i källkod

Entity: beginning of an implementation

Streetwalrus Einstein 10 år sedan
förälder
incheckning
d641558100
3 ändrade filer med 20 tillägg och 3 borttagningar
  1. 4 1
      include/Entity.h
  2. 11 2
      src/Entity.cpp
  3. 5 0
      src/main.cpp

+ 4 - 1
include/Entity.h

@@ -3,6 +3,7 @@
 
 #include "Rect.h"
 #include "Camera.h"
+#include "Tileset.h"
 
 namespace WalrusRPG
 {
@@ -15,9 +16,11 @@ namespace WalrusRPG
     {
       protected:
         WalrusRPG::Utils::Rect coords;
+        WalrusRPG::Tileset *tset;
+        unsigned sprite_id;
 
       public:
-        Entity();
+        Entity(int x, int y, unsigned w, unsigned h, WalrusRPG::Tileset *tset, unsigned sprite_id);
         ~Entity();
         void render(Camera &camera, unsigned dt) const;
         void update(unsigned dt);

+ 11 - 2
src/Entity.cpp

@@ -1,11 +1,12 @@
 #include "Entity.h"
 #include "misc.h"
+#include "Text.h"
 
 #define ENTITY WalrusRPG::Entity
 
-ENTITY::Entity()
+ENTITY::Entity(int x, int y, unsigned w, unsigned h, WalrusRPG::Tileset *tset, unsigned sprite_id)
+    : coords(x, y, w, h), tset(tset), sprite_id(sprite_id)
 {
-    // TODO
 }
 
 ENTITY::~Entity()
@@ -13,6 +14,14 @@ ENTITY::~Entity()
     // TODO if you allocate dynamically members
 }
 
+void ENTITY::render(Camera &camera, unsigned dt) const
+{
+    if (camera.is_visible(coords))
+    {
+        (*tset).render_tile(sprite_id, coords.x - camera.get_x(), coords.y - camera.get_y(), dt);
+    }
+}
+
 void ENTITY::update(unsigned dt)
 {
     UNUSED(dt);

+ 5 - 0
src/main.cpp

@@ -8,6 +8,7 @@
 #include "Camera.h"
 #include "Text.h"
 #include "misc.h"
+#include "sprites.h"
 
 using namespace WalrusRPG;
 using namespace WalrusRPG::Graphics;
@@ -35,6 +36,9 @@ void map_loop(unsigned x, unsigned y, Map &map)
     unsigned keep_running = 1;
     Camera camera((signed) x, (signed) y);
 
+    Tileset asdf(better_character, 9, 16);
+    Entity test_char(115, 90, 9, 16, &asdf, 0);
+
     while (keep_running)
     {
         if (isKeyPressed(KEY_NSPIRE_ESC))
@@ -49,6 +53,7 @@ void map_loop(unsigned x, unsigned y, Map &map)
             // TODO?: Preset color macros/consts?
             buffer_fill(pix);
             map.render(camera, 1);
+            test_char.render(camera, 1);
             print_string(
                 "WalrusRPG test build \001", 0, 0);