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