DEADSOFTWARE

game: mplats now move flags and items
authorfgsfds <pvt.fgsfds@gmail.com>
Fri, 21 Jan 2022 20:11:43 +0000 (23:11 +0300)
committerfgsfds <pvt.fgsfds@gmail.com>
Fri, 21 Jan 2022 20:11:43 +0000 (23:11 +0300)
src/game/g_items.pas
src/game/g_panel.pas

index 0aa9d560ab9b00cd002fbca4ac994b9f80a76ede..a69ceaaf811bd9f84f5b07ec41b0e6e2fcf11537 100644 (file)
@@ -44,6 +44,8 @@ Type
     NeedSend: Boolean;
 
     procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
     NeedSend: Boolean;
 
     procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right!
+    procedure getMapBox (out x, y, w, h: Integer); inline;
+    procedure moveBy (dx, dy: Integer); inline;
 
     property myid: Integer read arrIdx;
   end;
 
     property myid: Integer read arrIdx;
   end;
@@ -80,7 +82,7 @@ type
   TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
 
 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
   TItemEachAliveCB = function (it: PItem): Boolean is nested; // return `true` to stop
 
 function g_Items_ForEachAlive (cb: TItemEachAliveCB; backwards: Boolean=false): Boolean;
-
+function g_Items_NextAlive (startIdx: Integer): PItem;
 
 var
   gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
 
 var
   gItemsTexturesID: Array [1..ITEM_MAX] of DWORD;
@@ -137,6 +139,23 @@ begin
   NeedSend := NeedSend or (Obj.X <> Obj.oldX) or (Obj.Y <> Obj.oldY);
 end;
 
   NeedSend := NeedSend or (Obj.X <> Obj.oldX) or (Obj.Y <> Obj.oldY);
 end;
 
+procedure TItem.getMapBox (out x, y, w, h: Integer); inline;
+begin
+  x := Obj.X+Obj.Rect.X;
+  y := Obj.Y+Obj.Rect.Y;
+  w := Obj.Rect.Width;
+  h := Obj.Rect.Height;
+end;
+
+procedure TItem.moveBy (dx, dy: Integer); inline;
+begin
+  if (dx <> 0) or (dy <> 0) then
+  begin
+    Obj.X += dx;
+    Obj.Y += dy;
+    positionChanged();
+  end;
+end;
 
 // ////////////////////////////////////////////////////////////////////////// //
 const
 
 // ////////////////////////////////////////////////////////////////////////// //
 const
@@ -893,6 +912,19 @@ begin
   end;
 end;
 
   end;
 end;
 
+function g_Items_NextAlive (startIdx: Integer): PItem;
+var
+  idx: Integer;
+begin
+  result := nil;
+  if (ggItems = nil) or (startIdx >= High(ggItems)) then exit;
+  for idx := startIdx + 1 to High(ggItems) do
+    if ggItems[idx].alive and ggItems[idx].slotIsUsed then
+    begin
+      result := @ggItems[idx];
+      exit;
+    end;
+end;
 
 // ////////////////////////////////////////////////////////////////////////// //
 procedure g_Items_EmitPickupSound (idx: Integer);
 
 // ////////////////////////////////////////////////////////////////////////// //
 procedure g_Items_EmitPickupSound (idx: Integer);
index d236550df72b3023824d5840d82ae06711d8f9e0..52722d38b5b033d3275382c9f29490215c3d9306 100644 (file)
@@ -222,7 +222,7 @@ implementation
 
 uses
   {$INCLUDE ../nogl/noGLuses.inc}
 
 uses
   {$INCLUDE ../nogl/noGLuses.inc}
-  e_texture, g_basic, g_map, g_game, g_gfx, e_graphics, g_weapons, g_triggers,
+  e_texture, g_basic, g_map, g_game, g_gfx, e_graphics, g_weapons, g_triggers, g_items,
   g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams;
 
 const
   g_console, g_language, g_monsters, g_player, g_grid, e_log, geom, utils, xstreams;
 
 const
@@ -707,6 +707,8 @@ var
   gib: PGib;
   cor: TCorpse;
   mon: TMonster;
   gib: PGib;
   cor: TCorpse;
   mon: TMonster;
+  flg: PFlag;
+  itm: PItem;
   mpfrid: LongWord;
   ontop: Boolean;
   actMoveTrig: Boolean;
   mpfrid: LongWord;
   ontop: Boolean;
   actMoveTrig: Boolean;
@@ -850,6 +852,40 @@ begin
           end;
         end;
 
           end;
         end;
 
+        // move and push flags
+        if gGameSettings.GameMode = GM_CTF then
+          for f := FLAG_RED to FLAG_BLUE do
+          begin
+            flg := @gFlags[f];
+            if (flg.State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then continue;
+            px := flg.Obj.X+flg.Obj.Rect.X;
+            py := flg.Obj.Y+flg.Obj.Rect.Y;
+            pw := flg.Obj.Rect.Width;
+            ph := flg.Obj.Rect.Height;
+            if not g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then continue;
+            if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
+              if (pdx <> 0) or (pdy <> 0) then
+              begin
+                flg.Obj.X := flg.Obj.X + pdx;
+                flg.Obj.Y := flg.Obj.Y + pdy;
+                flg.NeedSend := true;
+              end;
+          end;
+
+        // move and push items
+        itm := g_Items_NextAlive(-1);
+        while itm <> nil do
+        begin
+          if itm.Fall then
+          begin
+            itm.getMapBox(px, py, pw, ph);
+            if g_Collide(px, py, pw, ph, cx0, cy0, cw, ch) then
+              if tryMPlatMove(px, py, pw, ph, pdx, pdy, squash, @ontop) then
+                itm.moveBy(pdx, pdy); // this will call `positionChanged()` for us
+          end;
+          itm := g_Items_NextAlive(itm.myId);
+        end;
+
         // collect monsters
         monCheckListUsed := 0;
         g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);
         // collect monsters
         monCheckListUsed := 0;
         g_Mons_ForEachAt(cx0, cy0, cw, ch, monCollect);