浏览代码

Protect Camera's members

Streetwalrus Einstein 10 年之前
父节点
当前提交
4c78aabaa0
共有 3 个文件被更改,包括 38 次插入14 次删除
  1. 11 7
      include/Camera.h
  2. 24 4
      src/Camera.cpp
  3. 3 3
      src/Map.cpp

+ 11 - 7
include/Camera.h

@@ -6,6 +6,12 @@ namespace WalrusRPG
 	class Camera
 	{
 		protected:
+			// So, how will track camera's position? Top left or center?
+			unsigned x; // You'll probably want to switch over signed coordonates.
+			unsigned y;
+			unsigned render_area_width; // What if you only want to display the map on a part of the screen?
+			unsigned render_area_height;
+
 			// Do you want to use classes for coordinates and allow them to have vector-based mathematics? With operator overriding that could be doable to have
 			// Vector2 a(3, 2); Vector2 b(1, 2); Vector3 c = a+b;
 			// Vector2 destination;
@@ -13,18 +19,16 @@ namespace WalrusRPG
 			// Vector2 acceleration;
 
 		public:
-			// TODO!: TEMPOARY PUBLIC VARIABLES. Need to make getter/setters.
-			// So, how will track camera's position? Top left or center?
-			unsigned x; // You'll probably want to switch over signed coordonates.
-			unsigned y;
-			unsigned render_area_width; // What if you only want to display the map on a part of the screen?
-			unsigned render_area_height;
-
 			Camera(unsigned x, unsigned y);
 			~Camera();
 			// This doesn't need any render as it's the utility which helps rendering. Unless you want to show debnug things.
 			// void render(float dt) const;
 			void update(unsigned dt);
+
+			void set_x(unsigned x);
+			unsigned get_x() const;
+			void set_y(unsigned y);
+			unsigned get_y() const;
 	};
 }
 

+ 24 - 4
src/Camera.cpp

@@ -26,9 +26,29 @@ void CAMERA::update(unsigned dt)
 		velocity += acceleration * dt;
 	 */
 
-	if (isKeyPressed(KEY_NSPIRE_5)) this->y++;
-	if (isKeyPressed(KEY_NSPIRE_8)) this->y--;
-	if (isKeyPressed(KEY_NSPIRE_6)) this->x++;
-	if (isKeyPressed(KEY_NSPIRE_4)) this->x--;
+	if (isKeyPressed(KEY_NSPIRE_5)) y++;
+	if (isKeyPressed(KEY_NSPIRE_8)) y--;
+	if (isKeyPressed(KEY_NSPIRE_6)) x++;
+	if (isKeyPressed(KEY_NSPIRE_4)) x--;
+}
+
+void CAMERA::set_x(unsigned x)
+{
+	this->x = x;
+}
+
+unsigned CAMERA::get_x() const
+{
+	return this->x;
+}
+
+void CAMERA::set_y(unsigned y)
+{
+	this->y = y;
+}
+
+unsigned CAMERA::get_y() const
+{
+	return this->y;
 }
 

+ 3 - 3
src/Map.cpp

@@ -28,8 +28,8 @@ void MAP::update(unsigned dt)
 void MAP::render(WalrusRPG::Camera &camera, unsigned dt) const
 {
 	UNUSED(dt);
-	unsigned offset_x = camera.x % 24 * -1;
-	unsigned offset_y = camera.y % 24 * -1;
+	unsigned offset_x = camera.get_x() % 24 * -1;
+	unsigned offset_y = camera.get_y() % 24 * -1;
 
 	Rect_t sprite;
 	sprite.y = 0;
@@ -40,7 +40,7 @@ void MAP::render(WalrusRPG::Camera &camera, unsigned dt) const
 	{
 		for (unsigned i = 0; i < 15; i++)
 		{
-			sprite.x = this->layer0[(camera.x / 24 + i) + (camera.y / 24 + j) * this->width] * 24;
+			sprite.x = this->layer0[(camera.get_x() / 24 + i) + (camera.get_y() / 24 + j) * this->width] * 24;
 			draw_sprite_sheet(tiles, offset_x + i * 24, offset_y + j * 24, &sprite);
 		}		
 	}