Unit gameObjectsPlayer

The code of gameObjectsPlayer

unit gameObjectsPlayer;
{
    Copyright (c) 2011 Christopher Winward
    
    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/
}
{$mode objfpc}{$H+}

interface

uses
  classes, SysUtils, sdl, gl, gameObjectsClassBase, gameInput, gameVariables,
  gameSoundUnit, gameObjectsSprite, gameClassGun, gameGraphicsUnit;

type TPlayer = class(TGameObject)
  private
    health                 : S16;
    moveSpeed              : U8;
    moveXSpeed, moveYSpeed : F32;
    arrayGuns              : array of TGun;
  public
    procedure Init;
    procedure Update;
    procedure Draw;
    procedure Hurt(healthX : S16);
    function GetGun(gunID : U16) : TGun;
    function GetHealth : S16;
end;

implementation

uses
  GameObjectsRock;

const
  maxXSpeed = 12;
  maxYSpeed = 9;

procedure TPlayer.Init;
var
  gunCount : U8;
begin
  InitGeneric;
  SetXY(200, 200);
  SetXYSpeed(0, 0);
  moveSpeed := 2;
  moveXSpeed := moveSpeed / 3; //By precalculating these values, it saves processing power later
  moveYSpeed := moveSpeed / 2;
  health := 100;
  SetSpriteGeneric('shipAnim', true);
  setSpriteFrameSize(512, 128, 64, 64);
  SetAnim(1, 4, 1);
  setFrameDelay(1 / 8);
  LoadSound('bullet', 'bullet');
  setLength(arrayGuns, NumGunPos + 1);
  for gunCount := 1 to NumGunPos do
    begin
      arrayGuns[gunCount] := TGun.create;
      arrayGuns[gunCount].init(gunCount, Fire, gunCount);
    end;
  arrayGuns[1].enable;
  arrayGuns[2].enable;
  arrayGuns[3].enable;
  arrayGuns[4].enable;
  arrayGuns[5].enable;
  arrayGuns[6].enable;

  arrayGuns[1].SetGunType(Fire);
  arrayGuns[2].SetGunType(laser);
  //arrayGuns[2].SetRotating(true);
  arrayGuns[3].SetGunType(laser);
  //arrayGuns[3].SetRotating(true);
  arrayGuns[4].SetGunType(lightBullet);
  //arrayGuns[4].SetRotating(true);
  arrayGuns[5].SetGunType(lightBullet);
  //arrayGuns[5].SetRotating(true);
  arrayGuns[6].SetGunType(Fire);
  arrayGuns[6].SetRotating(true);
end;

procedure TPlayer.Update;
var
  gunPos : U8;
  count : U8;
  crRkChk : ^TRock; //Current rock to check
  collideX, collideY : boolean;

begin
  if (paused = false) then
    begin
      SetXYSpeed(getXSpeed * 0.95, getYSpeed * 0.95, false, false); //Slow the ship
      if (GetRotationValue < 0) then
        SetRotation(1, true);
      if (GetRotationValue > 0) then
        SetRotation(-1, true);
      if (checkKeyState(SDLK_A)) then
        SetXYSpeed(- moveXSpeed, 0, true, true);
      if (checkKeyState(SDLK_D)) then
        SetXYSpeed(moveXSpeed, 0, true, true);
      if (checkKeyState(SDLK_W)) then
        SetXYSpeed(0, - moveYSpeed, true, true);
      if (checkKeyState(SDLK_S)) then
        SetXYSpeed(0, moveYSpeed, true, true);

      SetRotation(GetYSpeed * 2);

      if (checkMouseState(1)) then
        begin
          for gunPos := 1 to NumGunPos do
            begin
              arrayGuns[gunPos].fireGun;
            end;
          toggleMouseState(1);
        end;

      if (checkMouseState(3)) then
        begin
          //ArrayBullets[currentBullet].fireBullet(miniBullet);
        end;

      if (GetXSpeed >  maxXSpeed) then
        SetXYSpeed(maxXSpeed, 0, false, true);
      if (GetXSpeed < - maxXSpeed) then
        SetXYSpeed(- maxXSpeed, 0, false, true);
      if (GetYSpeed >  maxYSpeed) then
        SetXYSpeed(0, maxYSpeed, true, false);
      if (GetYSpeed < - maxYSpeed) then
        SetXYSpeed(0, - maxYSpeed, true, false);
      if (GetY > 400 - (getFrameHeight div 2)) then
        SetXY(0, - (getFrameHeight div 2), true, false);
      if (GetY < - (getFrameHeight div 2) ) then
        SetXY(0, 400 - (getFrameHeight div 2), true, false);
      if (GetX < 16) then
        SetXYSpeed(1, 0, true, true);
      if (GetX > 600) then
        SetXYSpeed(-1, 0, true, true);
      if (health > 200) then
        health := 200;
      if (health < 0) then
        health := 0;

      Draw;

      for count := 1 to NumGunPos do
        begin
          ArrayGuns[count].update;
        end;

      glDisable(gl_Blend);

      {glPushMatrix;
        glColor3f(1,0,0);
        glBegin(GL_QUADS);
          glVertex2f(GetX,GetY);
          glVertex2f(GetX,GetY+GetFrameHeight);
          glVertex2f(GetX+GetFrameWidth,GetY+GetFrameHeight);
          glVertex2f(GetX+GetFrameWidth,GetY);
        glEnd;
      glPopMatrix;}

      for count := 1 to maxRocks do
        begin
          collideX := false;
          collideY := false;
          crRkChk := @ArrayRocks[Count];

          //Check each side of the sprite to see if it's inside
          {if ( GetX > crRkChk^.GetX ) and ( GetX < crRkChk^.GetX + crRkChk^.GetFrameWidth ) then collideX := true;
          if ( GetX+GetFrameWidth > crRkChk^.GetX ) and ( GetX+GetFrameWidth < crRkChk^.GetX + crRkChk^.GetFrameWidth ) then collideX := true;
          if ( GetY > crRkChk^.GetY ) and ( GetY < crRkChk^.GetY + crRkChk^.GetFrameHeight ) then collideY := true;
          if ( GetY+GetFrameHeight > crRkChk^.GetY ) and ( GetY+GetFrameHeight < crRkChk^.GetY + crRkChk^.GetFrameHeight ) then collideY := true;
          }

          if (crRkChk^.GetX + (crRkChk^.GetFrameWidth / 2) < GetX + GetFrameWidth) and (crRkChk^.GetX + (crRkChk^.GetFrameWidth / 2) > GetX) then
            CollideX := true;
          if (crRkChk^.GetY + (crRkChk^.GetFrameHeight / 2) < GetY + GetFrameHeight) and (crRkChk^.GetY + (crRkChk^.GetFrameHeight / 2) > GetY) then
            CollideY := true;

          {glPushMatrix;
            glColor3f(0,1,0);
            glBegin(GL_QUADS);
              glVertex2f(crRkChk^.GetX,crRkChk^.GetY);
              glVertex2f(crRkChk^.GetX,crRkChk^.GetY+crRkChk^.GetFrameHeight);
              glVertex2f(crRkChk^.GetX+crRkChk^.GetFrameWidth,crRkChk^.GetY+crRkChk^.GetFrameHeight);
              glVertex2f(crRkChk^.GetX+crRkChk^.GetFrameWidth,crRkChk^.GetY);
            glEnd;
          glPopMatrix;}

          if (collideX and collideY) then
            begin
              if (crRkChk^.getDamage > 0) then
                begin
                  crRkChk^.Hurt(1000);
                  Hurt(crRkChk^.getDamage);
                end;
            end;
        end;
    end;
end;

procedure TPlayer.Draw;
var
  tempY : F32;
begin
  UpdateGeneric(SpriteUpdate);
  tempY := GetY;

  if ( GetY > (400 - getFrameHeight) ) then
    begin
      setXY(0, GetY - 400, true, false);
      UpdateGeneric(SpriteUpdate);
      setXY(0, tempY, true, false);
    end;

  if( GetY < 0 ) then
    begin
      setXY(0, GetY + 400, true, false);
      UpdateGeneric(SpriteUpdate);
      setXY(0, tempY, true, false);
    end;

  gfxDrawHalo(GetX + 8, GetY +(GetFrameHeight div 2), 18, 1.4, 1, 1, 0.2, 0.05, 0.01);
  gfxDrawHalo(GetX + 8, GetY + (GetFrameHeight div 2), 32, 1.4, 1, 1, 0.4, 0.1, 0.005);

end;

procedure TPlayer.hurt(healthX : S16);
begin
  health -= healthx;
end;

function TPlayer.GetGun(gunID : U16) : TGun;
begin
  result := arrayGuns[gunID];
end;

function TPlayer.GetHealth : S16;
begin
  result := health;
end;

end.

Remarks

Could you write a game that uses routines in these SpaceShooter units?

Programming - a skill for life!

by Christopher Winward: L6 Age ~16