comparison src/creep.c @ 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 08f2bcf3447f
comparison
equal deleted inserted replaced
13:d118fe8fb1db 14:5c7f33441e43
4 creep creeps[MAX_CREEPS]; 4 creep creeps[MAX_CREEPS];
5 u16 cur_creeps; 5 u16 cur_creeps;
6 extern u16 tilemap[40*28]; 6 extern u16 tilemap[40*28];
7 7
8 u16 distances[20*14]; 8 u16 distances[20*14];
9
10 const s16 speeds[NUM_SPECIES] = { 2 };
9 11
10 u16 spawn_creep(u8 species, s16 x, s16 y) 12 u16 spawn_creep(u8 species, s16 x, s16 y)
11 { 13 {
12 u16 index; 14 u16 index;
13 for (index = 0; index < MAX_SPRITE; index++) 15 for (index = 0; index < MAX_SPRITE; index++)
24 } 26 }
25 creeps[cur_creeps].index = index; 27 creeps[cur_creeps].index = index;
26 creeps[cur_creeps].health = 1000; 28 creeps[cur_creeps].health = 1000;
27 creeps[cur_creeps].species = species; 29 creeps[cur_creeps].species = species;
28 creeps[cur_creeps].direction = 0; 30 creeps[cur_creeps].direction = 0;
31 creeps[cur_creeps].targetx = x;
32 creeps[cur_creeps].targety = y;
29 return cur_creeps++; 33 return cur_creeps++;
30 } 34 }
31 35
32 typedef struct { 36 typedef struct {
33 u16 index; 37 u16 index;
94 } 98 }
95 points = new_points; 99 points = new_points;
96 } 100 }
97 } 101 }
98 102
99 void print_distances() 103 void print_distances(void)
100 { 104 {
101 u16 x,y,tindex,dindex,dist; 105 u16 x,y,tindex,dindex,dist;
102 for (y = 0; y < 14; y++) 106 for (y = 0; y < 14; y++)
103 { 107 {
104 dindex = y * 20; 108 dindex = y * 20;
124 } 128 }
125 } 129 }
126 } 130 }
127 } 131 }
128 } 132 }
133
134 void update_creeps(void)
135 {
136 s16 i, disty, distx, mdist;
137 for (i = cur_creeps-1; i >= 0; --i)
138 {
139 creep *cur = creeps + i;
140 SpriteDef * sprite = spriteDefCache + cur->index;
141 distx = sprite->posx - cur->targetx;
142 if (distx < 0)
143 distx = -distx;
144 disty = sprite->posy - cur->targety;
145 if (disty < 0)
146 disty = -disty;
147 mdist = distx > disty ? distx : disty;
148 if (mdist < speeds[cur->species])
149 {
150 s16 dindex = sprite->posx / 16 + (sprite->posy / 16) * 20;
151 if (distances[dindex])
152 {
153 u16 bestdist = 0xFFFF;
154 s16 bestindex = 0xFFFF,diff,next;
155 if (sprite->posx > 8 && distances[dindex-1] < bestdist)
156 {
157 bestindex = dindex-1;
158 bestdist = distances[bestindex];
159 }
160 if (sprite->posx < (320-16) && distances[dindex+1] < bestdist)
161 {
162 bestindex = dindex+1;
163 bestdist = distances[bestindex];
164 }
165 if (sprite->posy > 8 && distances[dindex-20] < bestdist)
166 {
167 bestindex = dindex-20;
168 bestdist = distances[bestindex];
169 }
170 if (sprite->posy < (224-16) && distances[dindex+20] < bestdist)
171 {
172 bestindex = dindex+20;
173 bestdist = distances[bestindex];
174 }
175 diff = bestindex-dindex;
176 next = bestindex + diff;
177 while (next >= 0 && next < 20*14 && distances[next] < bestdist)
178 {
179 bestindex = next;
180 bestdist = distances[bestindex];
181 }
182 cur->targetx = (bestindex % 20) * 16 + 4;
183 cur->targety = (bestindex / 20) * 16 + 4;
184 } else {
185 //creep is at the goal
186 VDP_setSpritePosition(cur->index, -0x80, -0x80);
187 cur_creeps--;
188 if (i != cur_creeps)
189 {
190 memcpy(cur, creeps+cur_creeps, sizeof(creep));
191 }
192 }
193 } else {
194 if (distx > disty) {
195 VDP_setSpritePosition(cur->index,
196 sprite->posx + (sprite->posx > cur->targetx ? -speeds[cur->species] : speeds[cur->species]),
197 sprite->posy);
198 } else {
199 VDP_setSpritePosition(cur->index,
200 sprite->posx,
201 sprite->posy + (sprite->posy > cur->targety ? -speeds[cur->species] : speeds[cur->species]));
202 }
203 }
204 }
205 }