TMap.RayCast

From Soldat Community Wiki
Revision as of 17:44, 22 August 2013 by Mighty (talk | contribs) (new page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
function RayCast(x1, y1, x2, y2: Single; Player, Flag, Bullet, CheckCollider: Boolean; Team: Byte): Boolean
 x1,y1: Coordinates of a starting point
 x2,y2: Coordinates of another point
 Player: if TRUE, Player collide polygons will be checked
 Flag: if TRUE, Flag collide polygons will be checked
 Bullet: if TRUE, Bullet collide polygons will be checked
 CheckCollider: if TRUE, colliders will be checked
 Team: if Player or Flag are TRUE, Team defines which team colliders are checked
 Result: FALSE if there are no colliders between the points, TRUE if there are

Description

This function will check whether there are colliders between two coordinates on the map.

Example

Below example check whether there is vision between a specified point and a player.
Works only for player with ID = 1.
To define 1st point, type "mark". To stop displaying, type "stop".

const
  TICKINTERVAL = 4;

var
  newObj: TNewMapObject;
  newX,newY: single;
  pX,pY: single;
  lastObj: byte;
  count: integer;
  
procedure OnTick(t: integer);
begin
  // create new object if X,Y not 0;
  if newX <> 0 then
  begin
    newObj := TNewMapObject.Create;
    newObj.Style := 16;
    newObj.X := newX;
    newObj.Y := newY;
    if lastObj > 0 then 
      if Map.Objects[lastObj].Active then 
        Map.Objects[lastObj].Kill;
    lastObj := Map.AddObject(newObj).ID;
    newObj.Free;
  end;
  // display result in bigtext for player 1
  if Players[1].Active then
  begin
    if newX <> 0 then
    begin
      pX := Players[1].X;
      pY := Players[1].Y;
      try
        if Map.RayCast(newX,newY,pX,pY,FALSE,FALSE,FALSE,FALSE,1) then
          Players[1].BigText(11,inttostr(count)+': TRUE',200,$FFFFFF,0.08,100,100)
        else
          Players[1].BigText(11,inttostr(count)+': FALSE',200,$FFFFFF,0.08,100,100);
      except
        Players[1].BigText(11,inttostr(count)+': ERROR',200,$FF0000,0.08,100,100);
        break;
      end;
      count := count + 1;
    end;
  end
  else // stop if player became inactive
    newX := 0;
end;
  
procedure OnSpeak(P: TActivePlayer; Text: string);
begin
  if lowercase(Text) = 'mark' then
  begin
    newX := P.X;
    newY := P.Y;
    P.BigText(10,'Done',200,$666666,0.08,100,80);
  end
  else if lowercase(Text) = 'stop' then
  begin
    newX := 0;
    newY := 0;
    if lastObj > 0 then
      if Map.Objects[lastObj].Active then
        Map.Objects[lastObj].Kill;
    count := 1;
  end;
  
end;
  
begin
  newX := 0;
  newY := 0;
  lastObj := 0;
  count := 1;
  
  Game.OnClockTick := @OnTick;
  Game.TickThreshold := TICKINTERVAL;
  Players[1].OnSpeak := @OnSpeak;
end.