changeset 21:bb7dfb42b320

Small refactor to object placement. Add spawn point object
author Michael Pavone <pavone@retrodev.com>
date Sun, 12 Jan 2014 23:53:18 -0800
parents 51a0972fcf76
children b725a715b358
files src/main.c src/map.c src/map.h
diffstat 3 files changed, 45 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.c	Sun Jan 12 22:43:03 2014 -0800
+++ b/src/main.c	Sun Jan 12 23:53:18 2014 -0800
@@ -42,11 +42,6 @@
 
 };
 
-#define EMPTY 0
-#define WALL 'O'-32 + TILE_FONTINDEX
-#define TOWER TILE_ATTR_FULL(1, 0, 0, 0, 'T'-32 + TILE_FONTINDEX)
-#define GOAL TILE_ATTR_FULL(1, 0, 0, 0, 'G'-32 + TILE_FONTINDEX)
-
 u16 countdown;
 
 int cursor_x = 0;  // tiles
@@ -79,18 +74,11 @@
 	}
 	if (went_down & BUTTON_A && !running) {
 		//u16 type_to_place = EMPTY;
-		u16 type_to_place = WALL;
-		tilemap[cursor_x     + (cursor_y    ) * 40] = type_to_place;
-		tilemap[cursor_x + 1 + (cursor_y    ) * 40] = type_to_place;
-		tilemap[cursor_x     + (cursor_y + 1) * 40] = type_to_place;
-		tilemap[cursor_x + 1 + (cursor_y + 1) * 40] = type_to_place;
+		place_object(WALL, cursor_x, cursor_y);
 		gen_distances(38, 14);
 		if (distances[122/16] == 0xFFFF)
 		{
-			tilemap[cursor_x     + (cursor_y    ) * 40] = 0;
-			tilemap[cursor_x + 1 + (cursor_y    ) * 40] = 0;
-			tilemap[cursor_x     + (cursor_y + 1) * 40] = 0;
-			tilemap[cursor_x + 1 + (cursor_y + 1) * 40] = 0;
+			place_object(EMPTY, cursor_x, cursor_y);
 		}
 	}
 	if (went_down & BUTTON_START)
@@ -116,16 +104,13 @@
 			tilemap[i+1] = WALL;
 		}
 	*/
-
-	tilemap[38 + 14*40] = GOAL;
-	tilemap[39 + 14*40] = GOAL;
-	tilemap[38 + 15*40] = GOAL;
-	tilemap[39 + 15*40] = GOAL;
+	place_object(GOAL, 38, 14);
+	place_object(SPAWN, 0, 14);
+	gen_distances(38, 14);
 	for (i = 0; i < MAX_SPRITE; i++)
 	{
 		spriteDefCache[i].posx = -0x80;
 	}
-	gen_distances(38, 14);
 	//print_distances();
 	for (;;)
 	{
@@ -142,7 +127,7 @@
 				--countdown;
 			else if (cur_creeps < 4)
 			{
-				spawn_creep(CREEP_NORMAL, 4, 122);
+				spawn_creep(CREEP_NORMAL, 0+4, 14*8+4);
 				countdown = 300;
 			}
 			update_creeps();
--- a/src/map.c	Sun Jan 12 22:43:03 2014 -0800
+++ b/src/map.c	Sun Jan 12 23:53:18 2014 -0800
@@ -10,31 +10,33 @@
 	u16 y;
 } mpoint;
 
+#define is_empty(tindex) (tilemap[tindex] == EMPTY_TA || tilemap[tindex] == SPAWN_UL)
+
 s16 explore(mpoint * points, s16 num_points, u16 src, u16 srcx, u16 srcy)
 {
 	u16 dist = distances[src]+1;
-	if (srcx < 19 && distances[src + 1] > dist && !tilemap[(srcx+1)*2+srcy*2*40])
+	if (srcx < 19 && distances[src + 1] > dist && is_empty((srcx+1)*2+srcy*2*40))
 	{
 		distances[src + 1] = dist;
 		points[num_points].index = src + 1;
 		points[num_points].x = srcx+1;
 		points[num_points++].y = srcy;
 	}
-	if (srcx && distances[src - 1] > dist && !tilemap[(srcx-1)*2 + srcy*2*40])
+	if (srcx && distances[src - 1] > dist && is_empty((srcx-1)*2 + srcy*2*40))
 	{
 		distances[src - 1] = dist;
 		points[num_points].index = src - 1;
 		points[num_points].x = srcx-1;
 		points[num_points++].y = srcy;
 	}
-	if (srcy < 13 && distances[src + 20] > dist && !tilemap[srcx*2+(srcy+1)*2*40])
+	if (srcy < 13 && distances[src + 20] > dist && is_empty(srcx*2+(srcy+1)*2*40))
 	{
 		distances[src + 20] = dist;
 		points[num_points].index = src + 20;
 		points[num_points].x = srcx;
 		points[num_points++].y = srcy+1;
 	}
-	if (srcy && distances[src - 20] > dist && !tilemap[srcx*2 + (srcy-1)*2*40])
+	if (srcy && distances[src - 20] > dist && is_empty(srcx*2 + (srcy-1)*2*40))
 	{
 		distances[src - 20] = dist;
 		points[num_points].index = src - 20;
@@ -101,3 +103,20 @@
 		}
 	}
 }
+
+u16 tileinfo[OBJECT_TYPES][4] = {
+	{EMPTY_TA, EMPTY_TA, EMPTY_TA, EMPTY_TA},
+	{WALL_UL, WALL_UL, WALL_UL, WALL_UL},
+	{TOWER_UL, TOWER_UL, TOWER_UL, TOWER_UL},
+	{GOAL_UL, GOAL_UL, GOAL_UL, GOAL_UL},
+	{SPAWN_UL, SPAWN_UL, SPAWN_UL, SPAWN_UL}
+};
+
+void place_object(u16 type, u16 x, u16 y)
+{
+	u16 tindex = x + y*40;
+	tilemap[tindex]      = tileinfo[type][0];
+	tilemap[tindex + 1]  = tileinfo[type][1];
+	tilemap[tindex + 40] = tileinfo[type][2];
+	tilemap[tindex + 41] = tileinfo[type][3];
+}
--- a/src/map.h	Sun Jan 12 22:43:03 2014 -0800
+++ b/src/map.h	Sun Jan 12 23:53:18 2014 -0800
@@ -1,9 +1,25 @@
 #ifndef MAP_H_
 #define MAP_H_
 
+enum {
+	EMPTY,
+	WALL,
+	TOWER,
+	GOAL,
+	SPAWN,
+	OBJECT_TYPES
+} object_type;
+
+#define EMPTY_TA 0
+#define WALL_UL  'O'-32 + TILE_FONTINDEX
+#define TOWER_UL TILE_ATTR_FULL(1, 0, 0, 0, 'T'-32 + TILE_FONTINDEX)
+#define GOAL_UL  TILE_ATTR_FULL(1, 0, 0, 0, 'G'-32 + TILE_FONTINDEX)
+#define SPAWN_UL TILE_ATTR_FULL(3, 0, 0, 0, 'S'-32 + TILE_FONTINDEX)
+
 extern u16 distances[20*14];
 extern u16 tilemap[40*28];
 void gen_distances(u16 x, u16 y);
 void print_distances(void);
+void place_object(u16 type, u16 x, u16 y);
 
 #endif //MAP_H_