bool addNewSurvivor(){ unsigned int wall = rand()%4; Position p; if(wall == 0){ p = (Position){ GRID_WIDTH-1, rand()%20 }; } else if(wall== 1){ p = (Position){ rand()%30, 0 }; } else if(wall == 2){ p = (Position){ rand()%30, GRID_HEIGHT-1}; } else{ p = (Position){ 0, rand()%20 }; } if(getEntityAt(p) == NULL){ grid[p.x][p.y] = createSurvivor(p); return true; } else{ return false; } }
bool shoot(Entity shooter, Position shotAt){ if(getEntityAt(shotAt)->type == ET_ZOMBIE){ if(shooter->remainingPoints > 0){ //if the random number is greater than the number of moves, the shot hits unsigned int distance = calculateDistance(shooter->position, shotAt); if((rand()%50) > distance + score*.1){ //hit Entity e = grid[shotAt.x][shotAt.y]; grid[shotAt.x][shotAt.y] = NULL; score++; freeEntity(e); } else{ //miss } shooter->remainingPoints -= 1; return true; } else{ return false; } } else{ return false; } }
//Draws the entities into the world void drawEntities() { Entity entityTemp; for(int i = 0; i < getEntityNum(); i++) { entityTemp = getEntityAt(i); PlaceModel(*entityTemp.mesh, entityTemp.position.x, entityTemp.position.y, entityTemp.position.z, entityTemp.scale.x, entityTemp.scale.y, entityTemp.scale.z, entityTemp.angle); } }
void moveZombie(Entity z){ int r = rand()%4; Position p = z->position; Entity target = NULL; for(signed int x = ( (signed int) p.x ) - 5; x <= ( (signed int) p.x ) + 5; x++) { if(x < 0 || x >= GRID_WIDTH) { continue; } for(signed int y = ( (signed int) p.y ) - 5; y <= ( (signed int) p.y ) + 5; y++) { if(y < 0 || y >= GRID_HEIGHT) { continue; } Entity e = getEntityAt((Position) { (unsigned int) x, (unsigned int) y }); if(e && e->type == ET_SURVIVOR) { if(target) { Position o = getPosition(target); Position u = getPosition(e); if(calculateDistance(p, u) < calculateDistance(p, o)) { target = e; } } else { target = e; } } } } if(target) { Position o = getPosition(target); if(calculateDistance(p, o) == 1) { // Attack target->remainingHealth--; if(target->remainingHealth == 0) { // Convert the heretics NumSurvivors--; score -= 3; target->type = ET_ZOMBIE; } } else if(o.y < p.y) { p.y--; } else if(o.y > p.y) { p.y++; } else if(o.x < p.x) { p.x--; } else if(o.x > p.x) { p.x++; } } else { if(r == 0){ p.x--; } else if(r==1){ p.y++; } else if(r==2){ p.x++; } else{ p.y--; } if(p.x >= GRID_WIDTH){ p.x = z->position.x;; } if(p.y >= GRID_HEIGHT){ p.y = z->position.y; } } if(z->position.x != p.x || z->position.y != p.y) { move(z,p); } }