Mercurial > repos > genjam1
diff 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 |
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])); + } + } + } +}