Showing posts with label Triggers. Show all posts
Showing posts with label Triggers. Show all posts

Friday, February 21, 2014

Time for triggers

The main change in this version is that I've added a triggering system, in the map editor one can add as many triggers as he wants.

There is a file for each level called triggers.map

[1]
x=92
y=59
ClearTile=-78,-54
ClearAttribute=0,0
PlaySound=wall.wav

The values mean:
  1. TriggerId
  2. Trigger x position
  3. Trigger y position
  4. Function to call and parameters

Then there is class called TTriggerDispatcher which contains all possible methods to be called, for instance:

procedure TTriggerDispatcher.PlaySound(aX, aY: integer; const aName: string);
begin
  TSoundDispatcher.Instance.Play('Sound\' + aName);
end;

procedure TTriggerDispatcher.SpawnEntity(aX, aY, aOfsX, aOfsY, aEntity: integer);
var
  info: TMapInfo;
  entity: ISprite;

begin
  info := TMapInfo.Create((aX + aOfsX) * FLayer.XTileSize, (aY + aOfsY) * FLayer.YTileSize, NO_TILE, aEntity);
  entity := TSpriteFactory.Instance.New(info);
  (entity as IWarp).Warp;
end;


The way that this is handled is via rtti:


procedure TGameTrigger.Invoke;
var
  info : TRttiType;
  method: TRttiMethod;
  params: TArray<TValue>;
  context: TRttiContext;
  par, i: integer;

begin
  if FTarget = '' then
    raise Exception.Create('Invalid target');

  context := TRttiContext.Create;
  info := context.GetType(TTriggerDispatcher);
  method := info.GetMethod(FTarget);
  if method = '' then
    raise Exception.Create(FTarget + ' does not exists');

  Setlength(params, FParams.Count + 2);
  params[0] := TValue.From<integer>(FX);
  params[1] := TValue.From<integer>(FY);
  for i := 0 to FParams.Count - 1 do
  begin
    if TryStrToInt(FParams[i], par)  then
      params[i + 2] := TValue.From<integer>(par)
    else
      params[i + 2] := TValue.From<string>(FParams[i])

  end;

  method.Invoke(TTriggerDispatcher.Instance, params);
end;

From the map editor one can design some game logic. All files should be at skydrive

 BB)
  • If no animation assigned in TAnimationSprite then the old exception is no longer happening
  • TCollisionMatrix no longer returns the same sprite repeated
  • New trigger system
  • Triggers can dispatch more than one method 
  • Map load/saves triggers
HBF)
  • New balls enemy 
  • New trigger entity
  • New music for level no name
  • CrushWalls fixed
  • When hero is finishing a level, immunity is activated
  • Possible triggers at the moment are: clear and set tiles and attributes, play sound and music and spawn entities
  • When in water and bubbles are shown and new sound is played
  • Triggers used in some levels