Skip to content

Commit 9320775

Browse files
committed
Store Items in a fixed plain array #8
1 parent 4411c4f commit 9320775

File tree

1 file changed

+54
-61
lines changed

1 file changed

+54
-61
lines changed

eepers.adb

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ with Ada.Strings.Unbounded;
77
use Ada.Strings.Unbounded;
88
with Ada.Containers.Vectors;
99
with Ada.Unchecked_Deallocation;
10-
with Ada.Containers.Hashed_Maps;
1110
use Ada.Containers;
1211
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
1312
with Ada.Strings;
@@ -210,33 +209,14 @@ procedure Eepers is
210209
return (A.X*S, A.Y*S);
211210
end;
212211

213-
function Equivalent_IVector2(Left, Right: IVector2) return Boolean is
214-
begin
215-
return Left.X = Right.X and then Left.Y = Right.Y;
216-
end;
217-
218-
function Hash_IVector2(V: IVector2) return Hash_Type is
219-
M31: constant Hash_Type := 2**31-1; -- a nice Mersenne prime
220-
begin
221-
return Hash_Type(V.X) * M31 + Hash_Type(V.Y);
222-
end;
223-
224-
type Item_Kind is (Item_Key, Item_Bomb_Gen, Item_Checkpoint);
225-
226-
type Item(Kind: Item_Kind := Item_Key) is record
227-
case Kind is
228-
when Item_Bomb_Gen =>
229-
Cooldown: Integer;
230-
when others => null;
231-
end case;
212+
type Item_Kind is (Item_None, Item_Key, Item_Bomb_Gen, Item_Checkpoint);
213+
type Item is record
214+
Kind: Item_Kind := Item_None;
215+
Position: IVector2;
216+
Cooldown: Integer;
232217
end record;
233-
234-
package Hashed_Map_Items is new
235-
Ada.Containers.Hashed_Maps(
236-
Key_Type => IVector2,
237-
Element_Type => Item,
238-
Hash => Hash_IVector2,
239-
Equivalent_Keys => Equivalent_IVector2);
218+
type Item_Index is range 1..100;
219+
type Item_Array is array (Item_Index) of Item;
240220

241221
function To_Vector2(iv: IVector2) return Vector2 is
242222
begin
@@ -325,7 +305,7 @@ procedure Eepers is
325305
Player_Bombs: Integer;
326306
Player_Bomb_Slots: Integer;
327307
Eepers: Eeper_Array;
328-
Items: Hashed_Map_Items.Map;
308+
Items: Item_Array;
329309
Bombs: Bomb_State_Array;
330310
end record;
331311

@@ -336,7 +316,7 @@ procedure Eepers is
336316

337317
Turn_Animation: Float := 0.0;
338318

339-
Items: Hashed_Map_Items.Map;
319+
Items: Item_Array;
340320
Bombs: Bomb_State_Array;
341321
Camera_Position: Vector2 := (x => 0.0, y => 0.0);
342322

@@ -497,9 +477,8 @@ procedure Eepers is
497477
Game.Bombs := Game.Checkpoint.Bombs;
498478
end;
499479

500-
Too_Many_Entities: exception;
501-
502480
function Allocate_Eeper(Game: in out Game_State) return Eeper_Index is
481+
Too_Many_Entities: exception;
503482
begin
504483
for Eeper in Game.Eepers'Range loop
505484
if Game.Eepers(Eeper).Dead then
@@ -510,6 +489,21 @@ procedure Eepers is
510489
raise Too_Many_Entities;
511490
end;
512491

492+
procedure Allocate_Item(Game: in out Game_State; Position: IVector2; Kind: Item_Kind) is
493+
Too_Many_Items: exception;
494+
begin
495+
Put_Line(Kind'Image);
496+
for Item of Game.Items loop
497+
if Item.Kind = Item_None then
498+
Item.Kind := Kind;
499+
Item.Position := Position;
500+
Item.Cooldown := 0;
501+
return;
502+
end if;
503+
end loop;
504+
raise Too_Many_Items;
505+
end;
506+
513507
procedure Spawn_Gnome(Game: in out Game_State; Position: IVector2) is
514508
Gnome: Eeper_State renames Game.Eepers(Allocate_Eeper(Game));
515509
Size: constant IVector2 := (1, 1);
@@ -646,7 +640,9 @@ procedure Eepers is
646640
end loop;
647641
end loop;
648642

649-
Game.Items.Clear;
643+
for Item of Game.Items loop
644+
Item.Kind := Item_None;
645+
end loop;
650646
for Bomb of Game.Bombs loop
651647
Bomb.Countdown := 0;
652648
end loop;
@@ -682,15 +678,15 @@ procedure Eepers is
682678
when Level_Door => Game.Map(Row, Column) := Cell_Door;
683679
when Level_Checkpoint =>
684680
Game.Map(Row, Column) := Cell_Floor;
685-
Game.Items.Insert((Column, Row), (Kind => Item_Checkpoint));
681+
Allocate_Item(Game, (Column, Row), Item_Checkpoint);
686682
when Level_Bomb_Gen =>
687683
Game.Map(Row, Column) := Cell_Floor;
688-
Game.Items.Insert((Column, Row), (Kind => Item_Bomb_Gen, Cooldown => 0));
684+
Allocate_Item(Game, (Column, Row), Item_Bomb_Gen);
689685
when Level_Barricade =>
690686
Game.Map(Row, Column) := Cell_Barricade;
691687
when Level_Key =>
692688
Game.Map(Row, Column) := Cell_Floor;
693-
Game.Items.Insert((Column, Row), (Kind => Item_Key));
689+
Allocate_Item(Game, (Column, Row), Item_Key);
694690
when Level_Player =>
695691
Game.Map(Row, Column) := Cell_Floor;
696692
if Update_Player then
@@ -754,23 +750,23 @@ procedure Eepers is
754750
end;
755751

756752
procedure Game_Items(Game: in Game_State) is
757-
use Hashed_Map_Items;
758753
begin
759-
for C in Game.Items.Iterate loop
760-
case Element(C).Kind is
761-
when Item_Key => Draw_Key(Key(C));
754+
for Item of Game.Items loop
755+
case Item.Kind is
756+
when Item_None => null;
757+
when Item_Key => Draw_Key(Item.Position);
762758
when Item_Checkpoint =>
763759
declare
764760
Checkpoint_Item_Size: constant Vector2 := Cell_Size*0.5;
765761
begin
766-
Draw_Rectangle_V(To_Vector2(Key(C))*Cell_Size + Cell_Size*0.5 - Checkpoint_Item_Size*0.5, Checkpoint_Item_Size, Palette_RGB(COLOR_CHECKPOINT));
762+
Draw_Rectangle_V(To_Vector2(Item.Position)*Cell_Size + Cell_Size*0.5 - Checkpoint_Item_Size*0.5, Checkpoint_Item_Size, Palette_RGB(COLOR_CHECKPOINT));
767763
end;
768764
when Item_Bomb_Gen =>
769-
if Element(C).Cooldown > 0 then
770-
Draw_Bomb(Key(C), Color_Brightness(Palette_RGB(COLOR_BOMB), -0.5));
771-
Draw_Number(Key(C), Element(C).Cooldown, Palette_RGB(COLOR_LABEL));
765+
if Item.Cooldown > 0 then
766+
Draw_Bomb(Item.Position, Color_Brightness(Palette_RGB(COLOR_BOMB), -0.5));
767+
Draw_Number(Item.Position, Item.Cooldown, Palette_RGB(COLOR_LABEL));
772768
else
773-
Draw_Bomb(Key(C), Palette_RGB(COLOR_BOMB));
769+
Draw_Bomb(Item.Position, Palette_RGB(COLOR_BOMB));
774770
end if;
775771
end case;
776772
end loop;
@@ -825,32 +821,30 @@ procedure Eepers is
825821
case Game.Map(New_Position.Y, New_Position.X) is
826822
when Cell_Floor =>
827823
Game.Player.Position := New_Position;
828-
declare
829-
use Hashed_Map_Items;
830-
C: Cursor := Game.Items.Find(New_Position);
831-
begin
832-
if Has_Element(C) then
833-
case Element(C).Kind is
824+
for Item of Game.Items loop
825+
if Item.Position = New_Position then
826+
case Item.Kind is
827+
when Item_None => null;
834828
when Item_Key =>
835829
Game.Player.Keys := Game.Player.Keys + 1;
836-
Game.Items.Delete(C);
830+
Item.Kind := Item_None;
837831
Play_Sound(Key_Pickup_Sound);
838832
when Item_Bomb_Gen => if
839833
Game.Player.Bombs < Game.Player.Bomb_Slots
840-
and then Element(C).Cooldown <= 0
834+
and then Item.Cooldown <= 0
841835
then
842836
Game.Player.Bombs := Game.Player.Bombs + 1;
843-
Game.Items.Replace_Element(C, (Kind => Item_Bomb_Gen, Cooldown => BOMB_GENERATOR_COOLDOWN));
837+
Item.Cooldown := BOMB_GENERATOR_COOLDOWN;
844838
Play_Sound(Bomb_Pickup_Sound);
845839
end if;
846840
when Item_Checkpoint =>
847-
Game.Items.Delete(C);
841+
Item.Kind := Item_None;
848842
Game.Player.Bombs := Game.Player.Bomb_Slots;
849843
Game_Save_Checkpoint(Game);
850844
Play_Sound(Checkpoint_Sound);
851845
end case;
852846
end if;
853-
end;
847+
end loop;
854848
when Cell_Door =>
855849
if Game.Player.Keys > 0 then
856850
Game.Player.Keys := Game.Player.Keys - 1;
@@ -1015,7 +1009,7 @@ procedure Eepers is
10151009
end if;
10161010
when Eeper_Gnome =>
10171011
Eeper.Dead := True;
1018-
Game.Items.Insert(Eeper.Position, (Kind => Item_Key));
1012+
Allocate_Item(Game, Eeper.Position, Item_Key);
10191013
end case;
10201014
end if;
10211015
end loop;
@@ -1160,12 +1154,11 @@ procedure Eepers is
11601154
end;
11611155

11621156
procedure Game_Items_Turn(Game: in out Game_State) is
1163-
use Hashed_Map_Items;
11641157
begin
1165-
for C in Game.Items.Iterate loop
1166-
if Element(C).Kind = Item_Bomb_Gen then
1167-
if Element(C).Cooldown > 0 then
1168-
Game.Items.Replace_Element(C, (Kind => Item_Bomb_Gen, Cooldown => Element(C).Cooldown - 1));
1158+
for Item of Game.Items loop
1159+
if Item.Kind = Item_Bomb_Gen then
1160+
if Item.Cooldown > 0 then
1161+
Item.Cooldown := Item.Cooldown - 1;
11691162
end if;
11701163
end if;
11711164
end loop;

0 commit comments

Comments
 (0)