Unit Enemies
The enemies unit of program Invader
unit enemies; { Copyright (c) 2011 Steven Binns Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License, as described at http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ } interface uses CRT, SDL, SDL_GFX, SYSUTILS, checks, globals; procedure DRAW_ENEMIES(screen : pSDL_SURFACE; level : integer); procedure CREATE_ENEMY(level : integer); procedure TICK_ENEMIES(mousex, mousey : integer); procedure MOVE_ENEMY(i, mousex, mousey : integer); var //'i' is a generic count variable for loops //'whichside' is a variable determining which side of the screen an enemy spawns on i, whichside, finx, finy, finr: integer; done : boolean; test_event : pSDL_EVENT; implementation procedure CREATE_ENEMY(level : integer); begin done := False; for i := 0 to 20 do begin if mobs[i, 0] = 0 then begin whichside := random(4); //select which side the enemy will spawn if whichside = 0 then begin mobsx[i] := 0; //at the very left of the screen mobsy[i] := random(401); //somewhere along the left border done := True; mobs[i, 0] := 1; //create the enemy mobs[i, 1] := random(3); //horizontal speed mobs[i, 2] := random(5) - 2;//vertical speed mobs[i, 3] := level+3; //sets the radius of the enemy mobs[i, 4] := 0; //Not chasing end; if whichside = 1 then begin mobsx[i] := 400; //at the very right of the screen mobsy[i] := random(401); //somewhere along the right border done := True; mobs[i, 0] := 1; mobs[i, 1] := random(3) - 2; mobs[i, 2] := random(5) - 2; mobs[i, 3] := level + 3; mobs[i, 4] := 0; end; if whichside = 2 then begin mobsx[i] := random(401);//somewhere along the top border mobsy[i] := 0; //at the very top of the screen done := True; mobs[i, 0] := 1; mobs[i, 1] := random(5) - 2; mobs[i, 2] := random(3); mobs[i, 3] := level + 3; mobs[i, 4] := 0; end; if whichside = 3 then begin mobsx[i] := random(401);//somewhere along the bottom border mobsy[i] := 400; //at the very bottom of the screen done := True; mobs[i, 0] := 1; mobs[i, 1] := random(5) - 2; mobs[i, 2] := random(3) - 2; mobs[i, 3] := level + 3; mobs[i, 4] := 0; end; end; if done = True then exit; end; end; procedure DRAW_ENEMIES(screen: pSDL_SURFACE; level: integer); begin for i := 0 to 20 do begin if mobs[i, 0] = 1 then //if the enemy exists begin FILLEDCIRCLECOLOR(screen, mobsx[i], mobsy[i], mobs[i, 3] DIV 2, $00FF00FF); //draw the enemy body if mobs[i, 4] = 0 then //if the enemy isn't chasing you begin FILLEDCIRCLECOLOR(screen, mobsx[i], mobsy[i], mobs[i, 3], $FFFFFF6F); //draw a white aggro circle end else if mobs[i, 4] = 1 then //if the enemy is chasing you begin FILLEDCIRCLECOLOR(screen, mobsx[i], mobsy[i], mobs[i, 3], $9600006F); //draw a red aggro circle if tick = 10 then //Increase the score by the combined radii of all the enemies chasing you (divided by 10). score := score + mobs[i, 3] DIV 10; end; end; end; SDL_FLIP(screen); //SDL function to refresh the screen SDL_DELAY(20); //Clear the enemies from the screen before the next frame. for i := 0 to 20 do begin if mobs[i, 0] = 1 then begin FILLEDCIRCLECOLOR(screen, mobsx[i], mobsy[i], mobs[i, 3], $000000FF); end; end; end; procedure TICK_ENEMIES(mousex, mousey : integer); //deal with the enemies for the current tick begin for i := 0 to 20 do //for each of the enemies possible begin checks.enemcollisioncheck(i); //check whether there are any collisions if mobs[i, 0] = 1 then //if the enemy exists begin if mobs[i, 1] = 0 then //if the x-speed is 0 begin if mobs[i, 2] = 0 then //if the y-speed is 0 begin //delete the enemy mobsx[i] := 0; mobsy[i] := 0; mobs[i, 0] := 0; mobs[i, 1] := 0; mobs[i, 2] := 0; mobs[i,3] := 0; mobs[i,4] := 0; end; end; MOVE_ENEMY(i, mousex, mousey); //move the enemy if mobsx[i] > 400 then //if it is off the screen to the right begin mobsx[i] := 0; mobsy[i] := 0; mobs[i, 0] := 0; mobs[i, 1] := 0; mobs[i, 2] := 0; mobs[i,3] := 0; mobs[i,4] := 0; end else if mobsy[i] > 400 then //if it is off the screen to the bottom begin mobsx[i] := 0; mobsy[i] := 0; mobs[i, 0] := 0; mobs[i, 1] := 0; mobs[i, 2] := 0; mobs[i, 3] := 0; mobs[i, 4] := 0; end else if mobsx[i] < 0 then //if it is off the screen to the left begin mobsx[i] := 0; mobsy[i] := 0; mobs[i, 0] := 0; mobs[i, 1] := 0; mobs[i, 2] := 0; mobs[i, 3] := 0; mobs[i, 4] := 0; end else if mobsy[i] < 0 then //if it is off the screen to the top begin mobsx[i] := 0; mobsy[i] := 0; mobs[i, 0] := 0; mobs[i, 1] := 0; mobs[i, 2] := 0; mobs[i, 3] := 0; mobs[i, 4] := 0; end; end; end; end; procedure MOVE_ENEMY(i, mousex, mousey : integer); begin if mobs[i, 4] = 0 then //if the enemy isn't chasing you begin mobsx[i] := mobsx[i] + mobs[i, 1]; //move its x position by its horizontal speed mobsy[i] := mobsy[i] + mobs[i, 2]; //move its y position by its vertical speed end else //if it is chasing you begin if mousex < mobsx[i] then //if the mouse is to its left dec(mobsx[i]) //move left by 1 position else if mousex > mobsx[i] then //if the mouse is to its right inc(mobsx[i]); //move right by 1 position if mousey < mobsy[i] then //if the mouse is above it dec(mobsy[i]) //move up by one position else if mousey > mobsy[i] then //if the mouse is below it inc(mobsy[i]); //move down by one position enemcollisioncheck(i); //check whether the chaser has reached you (check its collisions) end; end; begin new(test_event); end.