Code of web version of Unit AStar

Smart Mobile Studio code of unit AStar

unit AStar;
{
    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/

    Converted to run in Smart Mobile Studio by PPS (2013)
}

interface

uses 
  W3System, W3Components, W3Application, W3Game, W3GameApp, W3Graphics, uNodeClass, w3Lists;

type
  TApplication = class(TW3CustomGameApplication)
  protected
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;
    procedure KeyDownEvent(mCode : integer);
    procedure KeyUpEvent(mCode : integer);
  end;

implementation
var
  count, deleted : integer;
  newNodeLetter : String = 'G';
uses
  uNodeClass, uGlobalVariables, u_AStar_proc, uRendering;

procedure TApplication.KeyDownEvent(mCode : integer);
begin
  case mCode of
    27 : Application.Terminate;
    45 : insertButton := True;
    46 : deleteButton := true;
  end;
end;

procedure TApplication.KeyUpEvent(mCode : integer);
begin
  case mCode of
    45 : insertButton := False;
    46 : deleteButton := False;
  end;
end;

procedure TApplication.ApplicationStarting;
begin
  inherited;
  insertButton := False;
  deleteButton := False;
  asm
    window.onkeydown=function(e)
    {
    TApplication.KeyDownEvent(Self,e.keyCode);
    }
    window.onkeyup=function(e)
    {
    TApplication.KeyUpEvent(Self,e.keyCode);
    }

  end;
  KeyDownEvent(0);
  KeyUpEvent(0);

  GameView.OnMouseDown := procedure(o : TObject; b : TMouseButton; t : TShiftState; x, y : integer)
    begin
      mouseX := x;
      mouseY := y;
      case b of
        mbLeft : begin
                   mouse1Down := True;
                   mouse2Down := False
                 end;
        mbRight : begin
                    mouse1Down := False;
                    mouse2Down := True;
                  end;
      end;
    end;
  GameView.OnMouseUp := procedure(o : TObject; b : TMouseButton; t : TShiftState; x, y : integer)
    begin
      mouseX := x;
      mouseY := y;
      mouse1Down := False;
      mouse2Down := False;
    end;

  GameView.OnMouseMove := procedure (o : TObject; t : TShiftState; x, y : Integer)
    begin
      if Mouse1Down or Mouse2Down then
        begin
          mouseX := x;
          mouseY := y;
        end;
    end;
  GameView.Delay := 1;
  setupPreNodes;
  GameView.StartSession(True);
end;

procedure TApplication.ApplicationClosing;
begin
  GameView.EndSession;
  inherited;
end;

procedure TApplication.PaintView(Canvas: TW3Canvas);
begin
  // Clear background
  Canvas.FillStyle := 'rgb(180, 180, 255)';
  Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);
  //Add new nodes.
  if insertButton then
    begin
      insertButton := false; //So we don't accidentally spawn hundreds of nodes
      listOfNodes.Add(tNode.create(mouseX - 16, mouseY - 16, newNodeLetter));
      newNodeLetter := chr(ord(newNodeLetter) + 1); //Increment the new node letter.
    end;

    //Add node links

  //If an initial node has been selected for path linking ...
  if (selectedNode1 <> nil) and (mouse2Down = false) then
    begin
      if selectedNode2 <> nil then //If a second node was selected
        begin
          if not (((selectedNode1 = A) and  (selectedNode2 = F)) or
                  ((selectedNode1 = F) and (selectedNode2 = A))) then
            begin //We don't want a direct connection between A and F.
              //Link the two nodes if they aren't already linked.
              if selectedNode1.adjacentNodes.IndexOf(selectedNode2) = -1 then
                selectedNode1.addLink(selectedNode2);
              if selectedNode2.adjacentNodes.IndexOf(selectedNode1) = -1 then
                selectedNode2.addLink(selectedNode1);
            end;
        end;
      selectedNode1 := nil; //Clear up the selections.
      selectedNode2 := nil;
    end;

  //Update all the nodes.
  count := 0;
  deleted := 0;
  while (count - deleted < listOfNodes.Count) do
    begin   //updateNode returns a true if it needs to be deleted
      if tNode(listOfNodes[count - deleted]).updateNode = true then
        begin
          listOfNodes.Remove(listOfNodes.IndexOf(tNode(listOfNodes[count - deleted])));
          inc(deleted);
        end;
      inc(count);
    end;
  pathfoundList := findShortestPath(A, F, exploredListTemp);
  renderScene(Canvas);
end;

end.
Programming - a skill for life!

by Christopher Winward: U6 Age 17