and here is the
OnUpdateInput method:
Code:
procedure TTheProbe.OnUpdateInput(aTimeSlice: single; var aExecuting: boolean);
const
cFileName = 'untitled_level.lvl';
var
MouseX,MouseY : LongInt;
SelectedTile : PEngineObject;
MousePosValid : Boolean;
ClickedButton : TEditorButton;
Firing : Boolean;
KeyCode : LongInt;
MouseButton : LongInt;
GameControllerIndex : LongInt;
GameControllerButton : LongInt;
LeftDown : Boolean;
RightDown : Boolean;
XDiff : Integer;
YDiff : Integer;
x,y : Integer;
begin
// update any controls that need programming
if FUpdatingControl then
begin
xeEngine.Window.GetFirstHitInput(KeyCode,MouseButton,GameControllerIndex,GameControllerButton);
if KeyCode = KEY_ESCAPE then
FUpdatingControl := False
else
begin
if KeyCode <> -1 then
begin
if FControlToUpdate in[GameKey_MoveUp,GameKey_MoveDown,GameKey_MoveLeft,GameKey_MoveRight,GameKey_Fire] then
begin
FConfiguration.Controls[FControlToUpdate] := KeyCode;
FUpdatingControl := False;
end;
end;
if GameControllerButton <> -1 then
begin
if FControlToUpdate = GameJoy_Fire then
begin
FConfiguration.Controls[FControlToUpdate] := GameControllerButton;
FUpdatingControl := False;
end;
end;
end;
end;
if not FUpdatingControl and not Assigned(FRenderActiveGUI) and not FSingleLevelMode and (FDoors.Status = cDoors_Open) and (FGameMode = gmMenu) and Assigned(FActiveMenu) then
begin
if xeEngine.Window.KeyIsHit(FConfiguration.Controls[MenuKey_MoveUp]) or
GameControllerMovedUp then
FActiveMenu.MoveUp
else
if xeEngine.Window.KeyIsHit(FConfiguration.Controls[MenuKey_MoveDown]) or
GameControllerMovedDown then
FActiveMenu.MoveDown
else
if xeEngine.Window.KeyIsHit(FConfiguration.Controls[MenuKey_Execute]) or
(xeEngine.Window.KeyIsHit(KEY_KP_ENTER)) or
GameControllerFired(FConfiguration.Controls[GameJoy_Fire]) then
begin
FActiveMenu.ExecuteSelectedItem;
if FTerminated then aExecuting := False;
end;
end;
if not FUpdatingControl and not Assigned(FRenderActiveGUI) and not FShowEditorGUI then
begin
if (FGameMode = gmGamePlaying) and (FPlayerData.PlayerMode = pmNormal) then
GetPlayerInput
else
begin
if FGameMode = gmGamePlayerDead then
begin
Firing := PlayerFiring;
if Firing and (FProbeEngine.NumScansSinceProbeSeenAlive >= cMaxPlayerScansBeforeDead) then
begin
if not FSingleLevelMode and not FEditorEnabled and (FLives = 0) then
begin
// go back to menu
FDoors.Status := cDoors_Closing;
FLastGameMode := gmNone;
end
else
// no menu, so just restart game again
begin
ResetGame;
if FLives = 0 then
FLives := cDefaultLives;
FGameMode := gmGamePlaying;
end;
end;
end
else
if FGameMode = gmGameLevelComplete then
begin
Firing := PlayerFiring;
if Firing and (FLevelCompleteTimer <= 0) then
begin
NextLevel;
end;
end;
end;
if not FUpdatingControl and not Assigned(FRenderActiveGUI) and FEditorEnabled and (xeEngine.Window.KeyIsDown(KEY_LCTRL) or xeEngine.Window.KeyIsDown(KEY_RCTRL)) and xeEngine.Window.KeyIsHit(KEY_E) then
begin
FShowEditorGUI := True;
FPlayerData.PlayerMode := pmNormal;
if FConfiguration.MusicEnabled then
begin
if Assigned(FMenuMusic) then FMenuMusic.Resume;
if Assigned(FGameMusic) then FGameMusic.Stop;
end;
ResetGame;
end;
if not FUpdatingControl and not Assigned(FRenderActiveGUI) and xeEngine.Window.KeyIsHit(KEY_S) and (xeEngine.Window.KeyIsDown(KEY_LCTRL) or xeEngine.Window.KeyIsDown(KEY_RCTRL)) then
begin
// commit suicide since playing game
FProbeEngine.CommitSuicide;
end;
end
else
if not FUpdatingControl and not Assigned(FRenderActiveGUI) and FShowEditorGUI then
begin
// scroll editor level to allow clicking outside the level
// to place items and expand level automatically.
if xeEngine.Window.KeyIsDown(KEY_W) then
SetMapOffsetY(GetMapOffsetY - cEditorScrollSpeed * FTimeSlice)
else
if xeEngine.Window.KeyIsDown(KEY_S) then
SetMapOffsetY(GetMapOffsetY + cEditorScrollSpeed * FTimeSlice)
else
if xeEngine.Window.KeyIsDown(KEY_A) then
SetMapOffsetX(GetMapOffsetX - cEditorScrollSpeed * FTimeSlice)
else
if xeEngine.Window.KeyIsDown(KEY_D) then
SetMapOffsetX(GetMapOffsetX + cEditorScrollSpeed * FTimeSlice);
// previous/next tile list in editor
if xeEngine.Window.KeyIsHit(KEY_UP) then
PreviousTiles(cTileHeight + 1)
else
if xeEngine.Window.KeyIsHit(KEY_DOWN) then
NextTiles(cTileHeight + 1);
SelectedTile := GetSelectedTile;
if Assigned(SelectedTile) and (SelectedTile^.Id in cTurnableObjects) and (SelectedTile^.Value <> cObjTravelTube_ud) and (SelectedTile^.Value <> cObjTravelTube_lr) then
begin
if xeEngine.Window.KeyIsHit(KEY_SPACE) then
// rotate selected tile right
SelectedTile^.Attrib := NewDirection(SelectedTile^.Attrib,cTurnRight)
else
if (xeEngine.Window.KeyIsHit(KEY_X) and (SelectedTile^.Attrib in [cLeft,cRight])) or
(xeEngine.Window.KeyIsHit(KEY_Y) and (SelectedTile^.Attrib in [cUp,cDown])) then
// flip selected tile around perpendicular axis
begin
SelectedTile^.Attrib := NewDirection(NewDirection(SelectedTile^.Attrib,cTurnRight),cTurnRight);
end;
end;
xeEngine.Window.GetMousePos(MouseX,MouseY);
MousePosValid := (MouseX < cPageWidth) and (MouseY >= GetMapY) and (MouseY < (GetMapY + cPageHeight)) and not FScrolling;
if MousePosValid then
begin
MouseX := Floor((MouseX - GetMapX + GetMapOffsetX) / cTileWidth);
MouseY := Floor((MouseY - GetMapY + GetMapOffsetY) / cTileHeight);
if xeEngine.Window.KeyIsDown(KEY_LSHIFT) or xeEngine.Window.KeyIsDown(KEY_RSHIFT) then
// mode where starting or finishing a link
begin
if FActiveLinkInfo.PointIndex = 1 then
// draw active link to current mouse tile if valid
begin
FActiveLinkInfo.Points[1] := Position(MouseX,MouseY);
end;
if xeEngine.Window.MouseIsHit(cMouseButton_Left) then
begin
if FActiveLinkInfo.PointIndex = 0 then
begin
FActiveLinkInfo.PointIndex := 1;
FActiveLinkInfo.Points[0] := Position(MouseX,MouseY);
end
else
if FActiveLinkInfo.PointIndex = 1 then
begin
FActiveLinkInfo.PointIndex := 0;
FActiveLinkInfo.Points[1] := Position(MouseX,MouseY);
FProbeEngine.AddLink(FActiveLinkInfo.Points[0].x,FActiveLinkInfo.Points[0].y,
FActiveLinkInfo.Points[1].x,FActiveLinkInfo.Points[1].y);
end;
end
else
if xeEngine.Window.MouseIsHit(cMouseButton_Right) then
begin
FProbeEngine.DeleteLinksAt(MouseX,MouseY);
if FActiveLinkInfo.PointIndex = 1 then
begin
if (FActiveLinkInfo.Points[0].x = MouseX) and (FActiveLinkInfo.Points[0].y = MouseY) then
// delete active link as clearing links at same location as link start
FActiveLinkInfo.PointIndex := 0;
end;
end;
end
else
begin
FActiveLinkInfo.PointIndex := 0;
LeftDown := xeEngine.Window.MouseIsDown(cMouseButton_Left);
RightDown := xeEngine.Window.MouseIsDown(cMouseButton_Right);
if LeftDown or RightDown and Assigned(SelectedTile) then
begin
if MouseX < 0 then
// expand level left
begin
XDiff := Abs(MouseX);
FProbeEngine.InsertLeftColumns(XDiff);
MouseX := 0;
// update offset to support insertion
SetMapOffsetX(GetMapOffsetX + cTileWidth * xDiff);
end
else
if MouseX >= FProbeEngine.Width then
// expand level right
begin
XDiff := Abs(MouseX - FProbeEngine.Width) + 1;
FProbeEngine.InsertRightColumns(XDiff);
MouseX := FProbeEngine.Width - 1;
end;
if MouseY < 0 then
// expand level up
begin
YDiff := Abs(MouseY);
FProbeEngine.InsertTopRows(YDiff);
MouseY := 0;
// update offset to support insertion
SetMapOffsetY(GetMapOffsetY + cTileHeight * YDiff);
end
else
if MouseY >= FProbeEngine.Height then
// expand level down
begin
YDiff := Abs(MouseY - FProbeEngine.Height) + 1;
FProbeEngine.InsertBottomRows(YDiff);
MouseY := FProbeEngine.Height - 1;
end;
end;
if LeftDown then
begin
SelectedTile := GetSelectedTile;
if Assigned(SelectedTile) then
SetObjectAt(FProbeEngine,MouseX,MouseY,SelectedTile.Value);
end
else
if RightDown then
SetObjectAt(FProbeEngine,MouseX,MouseY,cObjSpace);
end;
end
else
begin
ClickedButton := GetLastEditorButton;
if ClickedButton = ebNewLevel then
// do new blank level
begin
LoadLevel(
[
'TTTTTTTTTTTTT',
'T T',
'T T',
'T T',
'T T',
'T P T',
'T 3 T',
'T 222 T',
'> 11111 <',
'TTTTTTTTTTTTT'
]
);
SetMapOffsetX(0);
SetMapOffsetY(0);
end
else
if ClickedButton = ebSaveLevel then
// TODO: add GUI to ask for location + filename
begin
if FileExists(FLevelName) then
FProbeEngine.SaveLevelToFile(FLevelName)
else
FProbeEngine.SaveLevelToFile(ExtractFilePath(ParamStr(0)) + cFileName);
end
else
if ClickedButton = ebLoadLevel then
// TODO: add GUI to ask for location + filename
begin
if FileExists(ExtractFilePath(ParamStr(0)) + cFileName) then
FProbeEngine.LoadLevelFromFile(ExtractFilePath(ParamStr(0)) + cFileName);
SetMapOffsetX(0);
SetMapOffsetY(0);
end
else
if ClickedButton = ebTestLevel then
begin
FProbeEngine.BackupLevel;
FShowEditorGUI := False;
ResetGame;
FGameMode := gmGamePlaying;
if FConfiguration.MusicEnabled then
begin
if Assigned(FMenuMusic) then FMenuMusic.Pause;
LoadMusicTrack(FMusicTrackOrder[FMusicTrack]);
end;
end
else
if ClickedButton = ebCountCrystals then
begin
FProbeEngine.CountCrystals;
end;
end;
end;
if xeEngine.Window.KeyIsHit(KEY_F5) then
xeEngine.Graphics.SaveTGAScreenShotToFile('screenshot_'+FormatDateTime('yyyymmdd_hhnnss',Now)+'.tga');
end;
As you can see it could use some refactoring! haha
cheers,
Paul
Bookmarks