Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

unit app_entity_powerup;

interface

uses sux_constant, sux_object, app_constant, app_entity_main,
     app_game_player, resource_texture;

type
  TPowerUp=record
    typus: SXState;
  end;
  SAEPowerUp=class(SAEntity)
    powerup: TPowerUp;

    procedure onRender; override;

    function getCrystalCount: SXInt;
    function getBombCount: SXInt;

    procedure appears;
    procedure setPowerUp(const powerup: SXState);
    procedure destroyAllCrystals;
    procedure collect;

    procedure onTimer; override;

    procedure initialize; override;
    constructor Create(parent:TObject);
    destructor Destroy; override;
  end;

implementation

uses main, app_entity_crystal, gl_main, OpenGL, app_particle_powerupappears;


// --- SAEPowerUp


procedure SAEPowerUp.onRender;
var size: SXFloat;
begin
  inherited;

  size := sx.math._sin(app.game.getGameTimer * 400) * 0.05;

  glPushMatrix;
  glTranslatef(settings.pos.x, settings.pos.y, 0);
  glScalef(1 + size, (1 - size), 1);

  gl.gl2D.renderPatternQuad(app.scene.board.settings.icon, SX_GL_BLEND_AVERAGE,
    sx.convert.ColorRGBOf(1,1,1), 0.5, 0, 0, 1, 1, 0, 0, 23);

  glPopMatrix;
end;




function SAEPowerUp.getCrystalCount: SXInt;
var e, count: SXInt;
begin
  count := 0;
  for e := 0 to app.game.entities.getMax do
  if (app.game.getEntity(e).settings.typus = SX_ENTITY_CRYSTAL) then inc(count);
  result := count;
end;


function SAEPowerUp.getBombCount: SXInt;
var e, count: SXInt;
begin
  count := 0;
  for e := 0 to app.game.entities.getMax do
  if (app.game.getEntity(e).settings.typus = SX_ENTITY_POWERUP) then
  begin
    if (SAEPowerUp(app.game.getEntity(e)).powerup.typus = SX_EPOWERUP_TYPUS_BOMB) then inc(count);
  end;

  result := count;
end;




procedure SAEPowerUp.appears;
var appear: SAPPowerUpAppears;
begin
  appear := SAPPowerUpAppears(app.game.createParticleSystem(SX_PARTICLESYSTEM_POWERUPAPPEARS));
  appear.createBubbles(getPos);
end;


procedure SAEPowerUp.setPowerUp(const powerup: SXState);
begin
  self.powerup.typus := powerup;
  case powerup of
    SX_EPOWERUP_TYPUS_PIXEL           : settings.texture := app.scene.board.settings.icon_pixel;
    SX_EPOWERUP_TYPUS_FLOWERS         : settings.texture := app.scene.board.settings.icon_flowers;
    SX_EPOWERUP_TYPUS_SLOWMOTION      : settings.texture := app.scene.board.settings.icon_slowmotion;
    SX_EPOWERUP_TYPUS_CREATECRYSTALS  : settings.texture := app.scene.board.settings.icon_createcrystals;
    SX_EPOWERUP_TYPUS_CONCAVE         : settings.texture := app.scene.board.settings.icon_concave;
    SX_EPOWERUP_TYPUS_BOMB            : settings.texture := app.scene.board.settings.icon_bomb;
  end;
end;


procedure SAEPowerUp.destroyAllCrystals;
var e: SXInt;
    entity: SAEntity;
begin
  for e := app.game.entities.getMax downto 0 do
  if (e <= app.game.entities.getMax) then
  begin
    entity := SAEntity(app.game.entities.getObject(e));
    if (entity is SAECrystal) then
    begin
      SAECrystal(entity).generateShards;
      app.game.deleteEntity(entity);
    end;
  end;
end;


procedure SAEPowerUp.collect;
begin
  case powerup.typus of
    SX_EPOWERUP_TYPUS_PIXEL           : app.game.settings.effects.pixel := 5;
    SX_EPOWERUP_TYPUS_FLOWERS         : app.game.settings.effects.flowers := 7;
    SX_EPOWERUP_TYPUS_SLOWMOTION      : app.game.settings.effects.slowmotion := 4;
    SX_EPOWERUP_TYPUS_CREATECRYSTALS  : app.game.settings.effects.createcrystals := 1;
    SX_EPOWERUP_TYPUS_CONCAVE         : app.game.settings.effects.concave := 10;
    SX_EPOWERUP_TYPUS_BOMB            : destroyAllCrystals;
  end;
  app.sound.playSample2D('sounds\pickup');
end;




procedure SAEPowerUp.onTimer;
begin
end;




procedure SAEPowerUp.initialize;
begin
end;


constructor SAEPowerUp.Create(parent:TObject);
var randomvalue: SXInt;
    totalrandom: SXInt;
    powerup: SXState;
begin
  inherited Create(parent);

  setupAppearance(nil, 20);
  setBlending(SX_GL_BLEND_ADDITIVE, 1.0);

  with app.ini.settings.gameplay do
  begin
    totalrandom := poweruprandomcrystal + poweruprandomconcave + poweruprandompixel
      + poweruprandomflowers + poweruprandomslowmotion + poweruprandombomb;
    randomvalue := sx.math.random.getRandomInt(1, totalrandom);

    // Select powerup by given chances.
    powerup := SX_NONE;
    if (randomvalue <= poweruprandomcrystal) then powerup := SX_EPOWERUP_TYPUS_CREATECRYSTALS
      else if (randomvalue <= poweruprandomcrystal + poweruprandomconcave) then powerup := SX_EPOWERUP_TYPUS_CONCAVE
        else if (randomvalue <= poweruprandomcrystal + poweruprandomconcave + poweruprandompixel) then powerup := SX_EPOWERUP_TYPUS_PIXEL
          else if (randomvalue <= poweruprandomcrystal + poweruprandomconcave + poweruprandompixel + poweruprandomflowers) then powerup := SX_EPOWERUP_TYPUS_FLOWERS
            else if (randomvalue <= poweruprandomcrystal + poweruprandomconcave + poweruprandompixel + poweruprandomflowers + poweruprandomslowmotion) then powerup := SX_EPOWERUP_TYPUS_SLOWMOTION
              else powerup := SX_EPOWERUP_TYPUS_BOMB;

    // Select bomb if enough crystals are present.
    if (getCrystalCount >= app.ini.settings.gameplay.spawnpowerupbomboncrystals) then
    if (getBombCount = 0) then powerup := SX_EPOWERUP_TYPUS_BOMB;

    if (powerup = SX_NONE) then powerup := SX_EPOWERUP_TYPUS_CREATECRYSTALS;
    setPowerUp(powerup);
  end;

  app.sound.playSample2D('sounds\powerup_appears');
  setFlipVertical(true);

  settings.pos.x := sx.math.random.getRandomFloat(-100, 100);
  settings.pos.y := sx.math.random.getRandomFloat(-100, 100);
  appears;
end;


destructor SAEPowerUp.Destroy;
begin
  inherited;
end;


end.