Skip to content

Commit 4411c4f

Browse files
committed
Queue Player Commands #4
1 parent 3bf2feb commit 4411c4f

File tree

2 files changed

+130
-62
lines changed

2 files changed

+130
-62
lines changed

eepers.adb

Lines changed: 125 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -934,15 +934,49 @@ procedure Eepers is
934934
return Vector2_Lerp(Prev_Position, Curr_Position, C_Float(1.0 - T*T));
935935
end;
936936

937-
Space_Pressed: Boolean := False;
938-
Dir_Pressed: array (Direction) of Boolean := [others => False];
937+
type Command_Kind is (Command_Step, Command_Plant);
938+
type Command(Kind: Command_Kind := Command_Step) is record
939+
case Kind is
940+
when Command_Step => Dir: Direction;
941+
when Command_Plant => null;
942+
end case;
943+
end record;
944+
Command_Capacity: constant Natural := 5;
945+
type Command_Array is array (0..Command_Capacity-1) of Command;
946+
type Command_Queue_Record is record
947+
Items: Command_Array;
948+
Start: Natural := 0;
949+
Size: Natural := 0;
950+
end record;
951+
952+
procedure Command_Enqueue(Q: in out Command_Queue_Record; C: Command) is
953+
begin
954+
Q.Items((Q.Start + Q.Size) mod Command_Capacity) := C;
955+
if Q.Size < Command_Capacity then
956+
Q.Size := Q.Size + 1;
957+
else
958+
Q.Start := (Q.Start + 1) mod Command_Capacity;
959+
end if;
960+
end;
961+
962+
function Command_Dequeue(Q: in out Command_Queue_Record; C: out Command) return Boolean is
963+
begin
964+
if Q.Size = 0 then
965+
return False;
966+
end if;
967+
C := Q.Items(Q.Start);
968+
Q.Size := Q.Size - 1;
969+
Q.Start := (Q.Start + 1) mod Command_Capacity;
970+
return True;
971+
end;
972+
973+
Command_Queue: Command_Queue_Record;
939974
Any_Key_Pressed: Boolean := False;
940975

941976
procedure Swallow_Player_Input is
942977
begin
943-
Space_Pressed := False;
978+
Command_Queue.Size := 0;
944979
Any_Key_Pressed := False;
945-
Dir_Pressed := [others => False];
946980
end;
947981

948982
procedure Game_Bombs_Turn(Game: in out Game_State) is
@@ -1221,53 +1255,57 @@ procedure Eepers is
12211255
return;
12221256
end if;
12231257

1224-
if Space_Pressed and then Game.Player.Bombs > 0 then
1225-
declare
1226-
Start_Of_Turn: constant Double := Get_Time;
1227-
begin
1228-
Game.Turn_Animation := 1.0;
1229-
Game_Explosions_Turn(Game);
1230-
Game_Items_Turn(Game);
1231-
1232-
-- Game_Player_Turn(Game, Action_Plant_Bomb, Left);
1233-
Game.Player.Prev_Eyes := Game.Player.Eyes;
1234-
Game.Player.Prev_Position := Game.Player.Position;
1235-
1236-
Game_Eepers_Turn(Game);
1237-
Game_Bombs_Turn(Game);
1238-
1239-
if Game.Player.Bombs > 0 then
1240-
for Bomb of Game.Bombs loop
1241-
if Bomb.Countdown <= 0 then
1242-
Bomb.Countdown := 3;
1243-
Bomb.Position := Game.Player.Position;
1244-
exit;
1245-
end if;
1246-
end loop;
1247-
Game.Player.Bombs := Game.Player.Bombs - 1;
1248-
Play_Sound(Plant_Bomb_Sound);
1249-
end if;
1258+
declare
1259+
C: Command;
1260+
begin
1261+
if Command_Dequeue(Command_Queue, C) then
1262+
case C.Kind is
1263+
when Command_Step =>
1264+
declare
1265+
Start_Of_Turn: constant Double := Get_Time;
1266+
begin
1267+
Game.Turn_Animation := 1.0;
1268+
Game_Explosions_Turn(Game);
1269+
Game_Items_Turn(Game);
1270+
Game_Player_Turn(Game, C.Dir);
1271+
Game_Eepers_Turn(Game);
1272+
Game_Bombs_Turn(Game);
1273+
Game.Duration_Of_Last_Turn := Get_Time - Start_Of_Turn;
1274+
end;
1275+
when Command_Plant =>
1276+
if Game.Player.Bombs > 0 then
1277+
declare
1278+
Start_Of_Turn: constant Double := Get_Time;
1279+
begin
1280+
Game.Turn_Animation := 1.0;
1281+
Game_Explosions_Turn(Game);
1282+
Game_Items_Turn(Game);
1283+
1284+
-- Game_Player_Turn(Game, Action_Plant_Bomb, Left);
1285+
Game.Player.Prev_Eyes := Game.Player.Eyes;
1286+
Game.Player.Prev_Position := Game.Player.Position;
1287+
1288+
Game_Eepers_Turn(Game);
1289+
Game_Bombs_Turn(Game);
1290+
1291+
if Game.Player.Bombs > 0 then
1292+
for Bomb of Game.Bombs loop
1293+
if Bomb.Countdown <= 0 then
1294+
Bomb.Countdown := 3;
1295+
Bomb.Position := Game.Player.Position;
1296+
exit;
1297+
end if;
1298+
end loop;
1299+
Game.Player.Bombs := Game.Player.Bombs - 1;
1300+
Play_Sound(Plant_Bomb_Sound);
1301+
end if;
12501302

1251-
Game.Duration_Of_Last_Turn := Get_Time - Start_Of_Turn;
1252-
end;
1253-
else
1254-
for Dir in Direction loop
1255-
if Dir_Pressed(Dir) then
1256-
declare
1257-
Start_Of_Turn: constant Double := Get_Time;
1258-
begin
1259-
Game.Turn_Animation := 1.0;
1260-
Game_Explosions_Turn(Game);
1261-
Game_Items_Turn(Game);
1262-
Game_Player_Turn(Game, Dir);
1263-
Game_Eepers_Turn(Game);
1264-
Game_Bombs_Turn(Game);
1265-
Game.Duration_Of_Last_Turn := Get_Time - Start_Of_Turn;
1266-
exit;
1267-
end;
1268-
end if;
1269-
end loop;
1270-
end if;
1303+
Game.Duration_Of_Last_Turn := Get_Time - Start_Of_Turn;
1304+
end;
1305+
end if;
1306+
end case;
1307+
end if;
1308+
end;
12711309
end;
12721310

12731311
procedure Game_Bombs(Game: Game_State) is
@@ -1381,7 +1419,6 @@ procedure Eepers is
13811419
Palette_Editor_Selected: Boolean := False;
13821420
Palette_Editor_Component: HSV_Comp := Hue;
13831421
Icon: Image;
1384-
13851422
begin
13861423
if not Change_Directory(Get_Application_Directory) then
13871424
Put_Line("WARNING: Could not change working directory to the application directory");
@@ -1425,19 +1462,45 @@ begin
14251462
Begin_Drawing;
14261463
Clear_Background(Palette_RGB(COLOR_BACKGROUND));
14271464

1428-
Swallow_Player_Input;
1429-
if Is_Key_Down(KEY_LEFT_SHIFT) then
1430-
Dir_Pressed(Left) := Boolean(Is_Key_Down(KEY_A)) or else Boolean(Is_Key_Down(KEY_LEFT));
1431-
Dir_Pressed(Right) := Boolean(Is_Key_Down(KEY_D)) or else Boolean(Is_Key_Down(KEY_RIGHT));
1432-
Dir_Pressed(Down) := Boolean(Is_Key_Down(KEY_S)) or else Boolean(Is_Key_Down(KEY_DOWN));
1433-
Dir_Pressed(Up) := Boolean(Is_Key_Down(KEY_W)) or else Boolean(Is_Key_Down(KEY_UP));
1465+
if Game.Player.Dead then
1466+
Command_Queue.Size := 0;
14341467
else
1435-
Dir_Pressed(Left) := Boolean(Is_Key_Pressed(KEY_A)) or else Boolean(Is_Key_Pressed(KEY_LEFT));
1436-
Dir_Pressed(Right) := Boolean(Is_Key_Pressed(KEY_D)) or else Boolean(Is_Key_Pressed(KEY_RIGHT));
1437-
Dir_Pressed(Down) := Boolean(Is_Key_Pressed(KEY_S)) or else Boolean(Is_Key_Pressed(KEY_DOWN));
1438-
Dir_Pressed(Up) := Boolean(Is_Key_Pressed(KEY_W)) or else Boolean(Is_Key_Pressed(KEY_UP));
1468+
if Boolean(Is_Key_Down(KEY_LEFT_SHIFT)) and then Game.Turn_Animation <= 0.0 then
1469+
if Is_Key_Down(KEY_A) or else Is_Key_Down(KEY_LEFT) then
1470+
Command_Queue.Size := 0;
1471+
Command_Enqueue(Command_Queue, (Kind => Command_Step, Dir => Left));
1472+
end if;
1473+
if Is_Key_Down(KEY_D) or else Is_Key_Down(KEY_RIGHT) then
1474+
Command_Queue.Size := 0;
1475+
Command_Enqueue(Command_Queue, (Kind => Command_Step, Dir => Right));
1476+
end if;
1477+
if Is_Key_Down(KEY_S) or else Is_Key_Down(KEY_DOWN) then
1478+
Command_Queue.Size := 0;
1479+
Command_Enqueue(Command_Queue, (Kind => Command_Step, Dir => Down));
1480+
end if;
1481+
if Is_Key_Down(KEY_W) or else Is_Key_Down(KEY_UP) then
1482+
Command_Queue.Size := 0;
1483+
Command_Enqueue(Command_Queue, (Kind => Command_Step, Dir => Up));
1484+
end if;
1485+
else
1486+
if Is_Key_Pressed(KEY_A) or else Is_Key_Pressed(KEY_LEFT) then
1487+
Command_Enqueue(Command_Queue, (Kind => Command_Step, Dir => Left));
1488+
end if;
1489+
if Is_Key_Pressed(KEY_D) or else Is_Key_Pressed(KEY_RIGHT) then
1490+
Command_Enqueue(Command_Queue, (Kind => Command_Step, Dir => Right));
1491+
end if;
1492+
if Is_Key_Pressed(KEY_S) or else Is_Key_Pressed(KEY_DOWN) then
1493+
Command_Enqueue(Command_Queue, (Kind => Command_Step, Dir => Down));
1494+
end if;
1495+
if Is_Key_Pressed(KEY_W) or else Is_Key_Pressed(KEY_UP) then
1496+
Command_Enqueue(Command_Queue, (Kind => Command_Step, Dir => Up));
1497+
end if;
1498+
end if;
1499+
if Is_Key_Pressed(KEY_SPACE) then
1500+
Command_Enqueue(Command_Queue, (Kind => Command_Plant));
1501+
end if;
14391502
end if;
1440-
Space_Pressed := Boolean(Is_Key_Pressed(KEY_SPACE));
1503+
Any_Key_Pressed := False;
14411504
while not Any_Key_Pressed and then Get_Key_Pressed /= KEY_NULL loop
14421505
Any_Key_Pressed := True;
14431506
end loop;

raylib.ads

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ package Raylib is
8585
Import => True,
8686
Convention => C,
8787
External_Name => "IsKeyPressed";
88+
function Is_Key_Released(key: int) return C_bool
89+
with
90+
Import => True,
91+
Convention => C,
92+
External_Name => "IsKeyReleased";
8893
function Get_Key_Pressed return int
8994
with
9095
Import => True,

0 commit comments

Comments
 (0)