1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
{
Copyright 2011-2017 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Player (TPlayer). }
unit GamePlayer;
interface
uses SysUtils, CastleImages, CastleGLUtils, CastleGLImages;
type
TDirection = (
dirNorth,
dirEast,
dirSouth,
dirWest,
dirNorthEast,
dirSouthEast,
dirSouthWest,
dirNorthWest);
TPlayer = class
private
MoveDistance: Single;
MoveStartTime: Single;
NewX, NewY: Cardinal;
NewXPixel, NewYPixel: Integer;
FX, FY: Cardinal;
FXPixel: Integer;
FYPixel: Integer;
FDirection: TDirection;
public
{ If Moving then he moves from (X, Y) position to (NewX, NewY).
MovingSmallMoveX, MovingSmallMoveX is the exact pixel displacement
of the player sprite then. }
Moving: boolean;
MovingSmallMoveX, MovingSmallMoveY: Single;
GLImage: array [TDirection] of TGLImage;
constructor Create;
destructor Destroy; override;
property X: Cardinal read FX;
property Y: Cardinal read FY;
{ These are calculated by CalculatePixelPosition. }
property XPixel: Integer read FXPixel;
property YPixel: Integer read FYPixel;
property Direction: TDirection read FDirection;
procedure Teleport(ANewX, ANewY: Cardinal; NewDirection: TDirection);
{ Calculate XPixel, YPixel once the Window sizes are known. }
procedure CalculatePixelPosition;
{ Note that ChangeX, ChangeY may point to invalid coords --- we will
correct (clamp) them as needed. }
procedure Move(ChangeX, ChangeY: Integer; NewDirection: TDirection); overload;
procedure Move(NewDirection: TDirection); overload;
procedure Update;
end;
implementation
uses CastleFilesUtils, CastleUtils,
GameWindow;
constructor TPlayer.Create;
const
MoveShortcutNames: array [TDirection] of string = (
'N',
'E',
'S',
'W',
'NE',
'SE',
'SW',
'NW');
var
Dir: TDirection;
begin
inherited Create;
for Dir := Low(Dir) to High(Dir) do
begin
GLImage[Dir] := TGLImage.Create(ApplicationData(
'tiles/woldforge/sprites/creatures/observer/observer_float_' +
MoveShortcutNames[Dir] + '_1_hh.png'));
end;
end;
destructor TPlayer.Destroy;
var
Dir: TDirection;
begin
for Dir := Low(Dir) to High(Dir) do
FreeAndNil(GLImage[Dir]);
inherited;
end;
procedure TPlayer.Teleport(ANewX, ANewY: Cardinal; NewDirection: TDirection);
begin
FX := ANewX;
FY := ANewy;
FDirection := NewDirection;
end;
procedure TPlayer.CalculatePixelPosition;
begin
ViewMoveToCenterPosition(X, Y, FXPixel, FYPixel);
end;
procedure TPlayer.Move(ChangeX, ChangeY: Integer; NewDirection: TDirection);
var
ANewX, ANewY: Cardinal;
begin
ANewX := Clamped(Integer(X) + ChangeX, 0, Map.Width - 1);
ANewY := Clamped(Integer(Y) + ChangeY, 0, Map.Height - 1);
if not Moving then
begin
{ Change Direction even if no move will be done --- this is a nice effect
visually when standing on the edge of the map and trying to move beside
the edge. }
FDirection := NewDirection;
if ((X <> ANewX) or
(Y <> ANewY)) then
begin
Moving := true;
NewX := ANewX;
NewY := ANewY;
ViewMoveToCenterPosition(NewX, NewY, NewXPixel, NewYPixel);
MoveStartTime := GameTime;
MovingSmallMoveX := 0;
MovingSmallMoveY := 0;
MoveDistance := Sqrt(Sqr(XPixel - NewXPixel) +
Sqr(YPixel - NewYPixel));
end;
end;
end;
procedure TPlayer.Move(NewDirection: TDirection);
begin
case NewDirection of
dirNorth: Move(0, 2, NewDirection);
dirEast : Move( 1, 0, NewDirection);
dirSouth: Move(0, -2, NewDirection);
dirWest : Move(-1, 0, NewDirection);
dirNorthEast: begin if Odd(Y) then Move(+1, 1, NewDirection) else Move(0, 1, NewDirection) end;
dirSouthEast: begin if Odd(Y) then Move(+1, -1, NewDirection) else Move(0, -1, NewDirection) end;
dirSouthWest: begin if not Odd(Y) then Move(-1, -1, NewDirection) else Move(0, -1, NewDirection) end;
dirNorthWest: begin if not Odd(Y) then Move(-1, 1, NewDirection) else Move(0, 1, NewDirection) end;
end;
end;
procedure TPlayer.Update;
var
Distance: Single;
const
PlayerMoveSpeed = 300.0;
begin
if Moving then
begin
Distance := (GameTime - MoveStartTime) * PlayerMoveSpeed;
if Distance > MoveDistance then
begin
FX := NewX;
FY := NewY;
FXPixel := NewXPixel;
FYPixel := NewYPixel;
Moving := false;
end else
begin
MovingSmallMoveX := MapRange(Distance, 0, MoveDistance,
{ It's XPixel - NewXPixel, not
NewXPixel - XPixel, because
our MovingSmallMoveX work in reverse fashion. }
0, XPixel - NewXPixel);
MovingSmallMoveY := MapRange(Distance, 0, MoveDistance,
0, YPixel - NewYPixel);
end;
end;
end;
end.
|