This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgame_controls.pas
194 lines (170 loc) · 5.71 KB
/
game_controls.pas
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
unit Game_controls;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, castleFilesUtils,
CastleGLImages,
CastleKeysMouse,
general_var;
type TControlsVariant=(controls_WASD,controls_TFGH,controls_IJKL,controls_cursor,{controls_numpad,}controls_numbers);
type FourKeys=record
up,down,left,right:TKey;
end;
type TPlayerControls=class(TObject)
public
MoveKeys,FireKeys:FourKeys;
MoveStyle,FireStyle:TControlsVariant;
FirePressed,MovePressed:boolean;
LastFireKeyPress,LastMoveKeyPress:TKey;
FireX,FireY,moveX,moveY:shortint;
constructor create;
procedure makeMoveControls(MoveControlsType:TControlsVariant);
procedure makeFireControls(FireControlsType:TControlsVariant);
procedure StopControls;
end;
var GameScreenStartX,GameScreenEndX,GameScreenStartY,GameScreenEndY:integer;
frameskip:integer=1;
PauseMode:boolean=false;
PlayerControls:array[0..3] of TPlayerControls; {2 and 3 are default controls for the Player1 and 0 is a mere link to those}
ControlsPic:array [TControlsVariant] of TGLImage;
procedure LoadControlsImages;
function GetControlImage(InputKeys:TControlsVariant):TGLImage;
function IncControlsStyle(inputControls:TControlsVariant):TcontrolsVariant;
Procedure cyclePlayer1Fire;
Procedure cyclePlayer1Move;
Procedure cyclePlayer2Fire;
Procedure cyclePlayer2move;
implementation
function IncControlsStyle(inputControls:TControlsVariant):TcontrolsVariant;
begin
case inputControls of
controls_WASD: result:=controls_TFGH;
controls_TFGH: result:=controls_IJKL;
controls_IJKL: result:=controls_cursor;
controls_cursor: result:=controls_numbers;
controls_numbers:result:=controls_WASD;
end;
end;
procedure LoadControlsImages;
begin
ControlsPic[controls_WASD]:=TGLImage.Create(ApplicationData(keyFolder+'WASD.png'),true);
ControlsPic[controls_TFGH]:=TGLImage.Create(ApplicationData(keyFolder+'TFGH.png'),true);
ControlsPic[controls_IJKL]:=TGLImage.Create(ApplicationData(keyFolder+'IJKL.png'),true);
ControlsPic[controls_cursor]:=TGLImage.Create(ApplicationData(keyFolder+'cursor.png'),true);
ControlsPic[controls_numbers]:=TGLImage.Create(ApplicationData(keyFolder+'numpad.png'),true);
end;
constructor TPlayerControls.create;
begin
FirePressed:=false;
MovePressed:=false;
LastFireKeyPress:=k_none;
LastMoveKeyPress:=k_none;
FireX:=0;
FireY:=0;
MoveX:=0;
MoveY:=0;
end;
const WASD_keys:FourKeys=(
up:k_w;
down:k_s;
left:k_a;
right:k_d);
const TFGH_keys:FourKeys=(
up:k_t;
down:k_g;
left:k_f;
right:k_h);
const IJKL_keys:FourKeys=(
up:k_i;
down:k_k;
left:k_j;
right:k_l);
const cursor_keys:fourKeys=(
up:k_up;
down:k_down;
left:k_left;
right:k_right);
const numbers_keys:FourKeys=(
up:k_numpad_8;
down:k_numpad_2;
left:k_numpad_4;
right:k_numpad_6);
{//numpad keys identical to cursor keys!!!
with numpad_keys do begin
up:=k_numpad_up;
down:=k_numpad_down;
left:=k_numpad_left;
right:=k_numpad_right;
end;}
procedure TPlayerControls.makeMoveControls(MoveControlsType:TControlsVariant);
begin
MoveStyle:=MoveControlsType;
case MoveControlsType of
controls_WASD: MoveKeys:=WASD_keys;
controls_TFGH: MoveKeys:=TFGH_keys;
controls_IJKL: MoveKeys:=IJKL_keys;
controls_cursor: MoveKeys:=cursor_keys;
{controls_numpad: MoveKeys:=numpad_keys;}
controls_numbers:MoveKeys:=numbers_keys;
end;
end;
procedure TPlayerControls.makeFireControls(FireControlsType:TControlsVariant);
begin
FireStyle:=FireControlsType;
case FireControlsType of
controls_WASD: FireKeys:=WASD_keys;
controls_TFGH: FireKeys:=TFGH_keys;
controls_IJKL: FireKeys:=IJKL_keys;
controls_cursor: FireKeys:=cursor_keys;
{controls_numpad: FireKeys:=numpad_keys;}
controls_numbers:FireKeys:=numbers_keys;
end;
end;
procedure TPlayerControls.stopControls;
begin
FirePressed:=false;
MovePressed:=false;
end;
function GetControlImage(InputKeys:TControlsVariant):TGLImage;
begin
result:=ControlsPic[InputKeys];
{ if inputKeys=controls_WASD then result:=ControlsPic[controls_WASD] else
if inputKeys=controls_TFGH then result:=ControlsPic[controls_TFGH] else
if inputKeys=controls_IJKL then result:=ControlsPic[controls_IJKL] else
if inputKeys=controls_cursor then result:=ControlsPic[controls_cursor] else
if inputKeys=controls_numbers then result:=ControlsPic[controls_numbers];}
end;
function DoControlsCollision(controlsStyle:TControlsVariant):TControlsVariant;
var tmpControls:TControlsVariant;
function checkControllsCollision(controlsStyle:TControlsVariant):boolean;
begin
if (controlsStyle<>playerControls[0].MoveStyle) and
(controlsStyle<>playerControls[0].FireStyle) and ((not player2Active) or (
(controlsStyle<>playerControls[1].MoveStyle) and
(controlsStyle<>playerControls[1].FireStyle))) then
result:=true else result:=false;
end;
begin
tmpControls:=ControlsStyle;
repeat
tmpControls:=IncControlsStyle(tmpControls);
until checkControllsCollision(tmpControls);
result:=tmpcontrols;
end;
Procedure cyclePlayer1Fire;
begin
playerControls[0].makeFireControls(DoControlsCollision(playerControls[0].FireStyle));
end;
Procedure cyclePlayer1Move;
begin
playerControls[0].makeMoveControls(DoControlsCollision(playerControls[0].MoveStyle));
end;
Procedure cyclePlayer2Fire;
begin
if Player2Active then playerControls[1].makeFireControls(DoControlsCollision(playerControls[1].FireStyle));
end;
Procedure cyclePlayer2move;
begin
if Player2Active then playerControls[1].makeMoveControls(DoControlsCollision(playerControls[1].MoveStyle));
end;
end.