changeset 14:5c7f33441e43

Creeps now move towards the goal
author Mike Pavone <pavone@retrodev.com>
date Sun, 12 Jan 2014 18:16:51 -0800
parents d118fe8fb1db
children f71eb24b3896
files src/creep.c src/creep.h src/main.c
diffstat 3 files changed, 91 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/creep.c	Sun Jan 12 17:19:42 2014 -0800
+++ b/src/creep.c	Sun Jan 12 18:16:51 2014 -0800
@@ -7,6 +7,8 @@
 
 u16 distances[20*14];
 
+const s16 speeds[NUM_SPECIES] = { 2 };
+
 u16 spawn_creep(u8 species, s16 x, s16 y)
 {
 	u16 index;
@@ -26,6 +28,8 @@
 	creeps[cur_creeps].health = 1000;
 	creeps[cur_creeps].species = species;
 	creeps[cur_creeps].direction = 0;
+	creeps[cur_creeps].targetx = x;
+	creeps[cur_creeps].targety = y;
 	return cur_creeps++;
 }
 
@@ -96,7 +100,7 @@
 	}
 }
 
-void print_distances()
+void print_distances(void)
 {
 	u16 x,y,tindex,dindex,dist;
 	for (y = 0; y < 14; y++)
@@ -126,3 +130,76 @@
 		}
 	}
 }
+
+void update_creeps(void)
+{
+	s16 i, disty, distx, mdist;
+	for (i = cur_creeps-1; i >= 0; --i)
+	{
+		creep *cur = creeps + i;
+		SpriteDef * sprite = spriteDefCache + cur->index;
+		distx = sprite->posx - cur->targetx;
+		if (distx < 0)
+			distx = -distx;
+		disty = sprite->posy - cur->targety;
+		if (disty < 0)
+			disty = -disty;
+		mdist = distx > disty ? distx : disty;
+		if (mdist < speeds[cur->species])
+		{
+			s16 dindex = sprite->posx / 16 + (sprite->posy / 16) * 20;
+			if (distances[dindex])
+			{
+				u16 bestdist = 0xFFFF;
+				s16 bestindex = 0xFFFF,diff,next;
+				if (sprite->posx > 8 && distances[dindex-1] < bestdist)
+				{
+					bestindex = dindex-1;
+					bestdist = distances[bestindex];
+				}
+				if (sprite->posx < (320-16) && distances[dindex+1] < bestdist)
+				{
+					bestindex = dindex+1;
+					bestdist = distances[bestindex];
+				}
+				if (sprite->posy > 8 && distances[dindex-20] < bestdist)
+				{
+					bestindex = dindex-20;
+					bestdist = distances[bestindex];
+				}
+				if (sprite->posy < (224-16) && distances[dindex+20] < bestdist)
+				{
+					bestindex = dindex+20;
+					bestdist = distances[bestindex];
+				}
+				diff = bestindex-dindex;
+				next = bestindex + diff;
+				while (next >= 0 && next < 20*14 && distances[next] < bestdist)
+				{
+					bestindex = next;
+					bestdist = distances[bestindex];
+				}
+				cur->targetx = (bestindex % 20) * 16 + 4;
+				cur->targety = (bestindex / 20) * 16 + 4;
+			} else {
+				//creep is at the goal
+				VDP_setSpritePosition(cur->index, -0x80, -0x80);
+				cur_creeps--;
+				if (i != cur_creeps)
+				{
+					memcpy(cur, creeps+cur_creeps, sizeof(creep));
+				}
+			}
+		} else {
+			if (distx > disty) {
+				VDP_setSpritePosition(cur->index,
+					sprite->posx + (sprite->posx > cur->targetx ? -speeds[cur->species] : speeds[cur->species]),
+					sprite->posy);
+			} else {
+				VDP_setSpritePosition(cur->index,
+					sprite->posx,
+					sprite->posy + (sprite->posy > cur->targety ? -speeds[cur->species] : speeds[cur->species]));
+			}
+		}
+	}
+}
--- a/src/creep.h	Sun Jan 12 17:19:42 2014 -0800
+++ b/src/creep.h	Sun Jan 12 18:16:51 2014 -0800
@@ -2,13 +2,15 @@
 #define CREEP_H_
 
 enum {
-	CREEP_NORMAL = 0
+	CREEP_NORMAL = 0,
+	NUM_SPECIES
 } creep_species;
 
 typedef struct {
 	u16  index;
 	u16  health;
-	u16  waypoint;
+	s16  targetx;
+	s16  targety;
 	u8   species;
 	u8   direction;
 } creep;
@@ -16,5 +18,8 @@
 #define MAX_CREEPS 40
 extern u16 cur_creeps;
 u16 spawn_creep(u8 species, s16 x, s16 y);
+void gen_distances(u16 x, u16 y);
+void print_distances(void);
+void update_creeps(void);
 
 #endif //CREEP_H_
--- a/src/main.c	Sun Jan 12 17:19:42 2014 -0800
+++ b/src/main.c	Sun Jan 12 18:16:51 2014 -0800
@@ -99,16 +99,14 @@
 			VDP_setTileMapRectByIndex(VDP_PLAN_B, tilemap + i*40, i*64, 40, 0);
 		}
 		VDP_setSprite(0, cursor_x, cursor_y, SPRITE_SIZE(2,2), TILE_ATTR_FULL(PAL0, 1, 0, 0, cursor_tile_index), spriteDefCache[0].link);
-		if (cur_creeps < 4)
+		if (countdown)
+			--countdown;
+		else if (cur_creeps < 4)
 		{
-			if (countdown)
-				--countdown;
-			else
-			{
-				spawn_creep(CREEP_NORMAL, 4, 122);
-				countdown = 300;
-			}
+			spawn_creep(CREEP_NORMAL, 4, 122);
+			countdown = 300;
 		}
+		update_creeps();
 	}
 	return 0;
 }