Unit Checks

by Steven: Y11 Age ~15

unit checks;
{
    Copyright (c) 2011 Steven

    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
  Classes, SysUtils, SDL, CRT, globals;

procedure CHECK_COLLISIONS(screen : pSDL_SURFACE; level, mousex, mousey : integer);
function COLLISION_CIRCLE(x1, y1, r1, x2, y2, r2 : integer) : boolean;
procedure enemcollisioncheck(exclude : integer);

var
  x1, y1, r1, x2, y2, r2, i : integer;
  test_event : pSDL_EVENT;

implementation

procedure enemcollisioncheck(exclude : integer); //checks whether enemies have collided with something
begin
  for i := 0 to 20 do //for each of the possible enemies
    begin
      if i <> exclude then //if it isn't the one being checked
        begin
          if mobs[exclude, 4] = 1 then  //if the one being checked is chasing you
            begin
              if mobs[i, 4] = 1 then //if the current enemy exists
                begin
                  if COLLISION_CIRCLE(mobsx[exclude], mobsy[exclude], mobs[exclude, 3],
                                      mobsx[i], mobsy[i], mobs[i, 3]) = true then //run circle collision algorithm
                    begin
                      mobs[exclude, 3] += mobs[i, 3]; //'absorb' the current enemy, increasing the radius of the enemy being checked
                      //destroy the current enemy since it has been 'absorbed'
                      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;
                      exit;
                    end;
                end;
            end;
        end;
    end;
end;

procedure CHECK_COLLISIONS(screen : pSDL_SURFACE; level, mousex, mousey : integer); //check whether ANYTHING has collided
begin
  for i := 0 to 20 do
    begin
      if mobs[i, 0] = 1 then //if the current enemy exists
        begin
          if COLLISION_CIRCLE(mobsx[i], mobsy[i], mobs[i, 3], mousex, mousey, level) = true //if the enemy's aggro circle has collided with yours
            then
              begin
                mobs[i, 4] := 1; //make it chase you
              end;
          if COLLISION_CIRCLE(mobsx[i], mobsy[i], mobs[i, 3] DIV 2, mousex, mousey, level DIV 5) = true then //if the enemy's body has collided with yours
            begin
              stop := true //start the losing sequence
            end;
        end;
    end;
end;

function COLLISION_CIRCLE(x1, y1, r1, x2, y2, r2 : integer): boolean; //circle collision algorithm
var
  finx : integer;
  finy : integer;
begin
   finx := x1 - x2; //the horizontal distance between the targets
   finy := y1 - y2; //the verticle distance between the targets
   if sqr(finx) + sqr(finy) <= (sqr(r1) + sqr(r2)) * 2 then //if the true distance between the targets is smaller than the radii of the two targets combined
     begin
       COLLISION_CIRCLE:= TRUE; //return true (they have collided)
     end
   else  //if the true distance is larger than the combined radii
     begin
       COLLISION_CIRCLE := FALSE; //return false(they havent collided)
     end;
  end;

begin
  new(test_event);
end.

Remarks

Could you "ring the changes" on this program to make an effective animation?

Programming - a skill for life!

by Steven: Y11 Age ~15