Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

unit app_entity_crystal;

interface

uses sux_constant, sux_object, app_constant, app_entity_main,
     app_game_player;

type
  TCrystal = record
    growspeed: SXFloat;
    maxsize: SXFloat;
    grown: SXBool;
    spawntimer: SXFloat;
    growfromdirection: SXVertex2D;
    spawntries: SXInt;
  end;
  SAECrystal=class(SAEntity)
    crystal: TCrystal;

    procedure onRender; override;

    procedure generateShards;
    procedure spawnChild(const offset: SXVertex2D);
    procedure setSpawnParameters(const initialsize, maxsize, growspeed: SXFloat);

    function getBoundingCircleSize: SXFloat; override;

    procedure grow;
    procedure spawn;
    procedure onTimer; override;

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

implementation

uses main, app_particle_crystalshards, resource_texture;


// --- SAECrystal


procedure SAECrystal.onRender;
var realpos: SXVertex2D;
    size: SXFloat;
begin
  realpos := settings.pos;
  settings.rot := 360 - settings.rot;
  size := settings.size;
  if (not crystal.grown) then
  begin
    settings.pos := sx.math.addVertices(settings.pos, sx.math.multiplyVertex(crystal.growfromdirection, crystal.maxsize-settings.size));
  end;
  settings.size := settings.size * 1.2;

  inherited;

  settings.pos := realpos;
  settings.rot := 360 - settings.rot;
  settings.size := size;
end;




procedure SAECrystal.generateShards;
var shards: SAPCrystalShards;
    s: SXInt;
    texture: SRTexture;
begin
  for s := 1 to 5 do
  begin
    texture := app.scene.board.settings.shards[s];
    shards := SAPCrystalShards(app.game.createParticleSystem(SX_PARTICLESYSTEM_CRYSTALSHARDS));
    shards.createShards(getPos, texture);
  end;
end;


procedure SAECrystal.spawnChild(const offset: SXVertex2D);
var newposition: SXVertex2D;
    direction, normal: SXVertex2D;
    child: SAECrystal;
    childmaxsize: SXFloat;
    oldpos: SXVertex2D;
    cannotspawn: SXBool;
begin
  childmaxsize := crystal.maxsize * 0.9;
  if (childmaxsize < 8) then exit;

  direction := sx.math.multiplyVertex(getVectorFromAngle(settings.rot), settings.size);
  normal.y := -direction.x;
  normal.x := direction.y;

  newposition := sx.math.addVertices(getPos, sx.math.multiplyVertex(direction, offset.y));
  newposition := sx.math.addVertices(newposition, sx.math.multiplyVertex(normal, offset.x));

  // Check if there is enough space for a new crystal.
  oldpos := settings.pos;
  settings.pos := newposition;
  cannotspawn := (intersectWithAnyBoundingCircles) or (isOutOfBoard);
  settings.pos := oldpos;
  if (cannotspawn) then exit;

  child := SAECrystal(app.game.createEntity(SX_ENTITY_CRYSTAL));
  child.setPosition(newposition);
  child.setRotation(sx.math.random.getRandomFloat(0, 360));
  child.setSpawnParameters(0, childmaxsize, crystal.growspeed);
  child.crystal.growfromdirection := sx.math.normalizeVector(sx.math.getPointVector(child.settings.pos, settings.pos));
end;


procedure SAECrystal.setSpawnParameters(const initialsize, maxsize, growspeed: SXFloat);
begin
  settings.size := initialsize;
  crystal.maxsize := maxsize;
  crystal.growspeed := growspeed;
  crystal.grown := false;
  crystal.spawntimer := 0;
  crystal.spawntries := 0;
end;




function SAECrystal.getBoundingCircleSize: SXFloat;
begin
  result := crystal.maxsize * 0.8;
end;




procedure SAECrystal.grow;
begin
  sx.timer.incTimer(settings.size, crystal.growspeed);
  if (settings.size >= crystal.maxsize) then
  begin
    crystal.grown := true;
    settings.size := crystal.maxsize;
  end;
end;


procedure SAECrystal.spawn;
var angle: SXFloat;
    offset: SXVertex2D;
 begin
  sx.timer.incTimer(crystal.spawntimer, crystal.growspeed);
  if (crystal.spawntimer > 10) and (crystal.spawntries < 10) then
  begin
    inc(crystal.spawntries);
    crystal.spawntimer := 0;
    angle := sx.math.random.getRandomFloat(0, 360);
    offset.x := sx.math._sin(angle) * 1.75;
    offset.y := sx.math._cos(angle) * 1.75;
    spawnChild(offset);
    {spawnChild(sx.convert.Vertex2DOf(0, 2));
    spawnChild(sx.convert.Vertex2DOf(0, -2));
    spawnChild(sx.convert.Vertex2DOf(1.514, 1.0));
    spawnChild(sx.convert.Vertex2DOf(1.514, -1.0));
    spawnChild(sx.convert.Vertex2DOf(-1.514, 1.0));
    spawnChild(sx.convert.Vertex2DOf(-1.514, -1.0));}

  end;
end;


procedure SAECrystal.onTimer;
begin
  if (not crystal.grown) then grow
    else spawn;
end;




procedure SAECrystal.initialize;
begin
end;


constructor SAECrystal.Create(parent:TObject);
begin
  inherited Create(parent);

  setSolid(true);
  setupAppearance(app.scene.board.settings.crystal, 20);
  setBlending(SX_GL_BLEND_AVERAGE, 1.0);

  crystal.growfromdirection := sx.convert.Vertex2DOf(0, 0);
end;


destructor SAECrystal.Destroy;
begin
  inherited;
end;


end.