gnoga_2.1.2_5f127c56/demo/leaves/leaves_main.adb

  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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
--  Leaves_main: GNOGA application with a "match-3"-style game.
--  All pictures are taken by the author with a Nokia 301 phone, some are reworked with GIMP.

--  Legal licensing note:

--  Copyright (c) 2017 Gautier de Montmollin

--  Permission is hereby granted, free of charge, to any person obtaining a copy
--  of this software and associated documentation files (the "Software"), to deal
--  in the Software without restriction, including without limitation the rights
--  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
--  copies of the Software, and to permit persons to whom the Software is
--  furnished to do so, subject to the following conditions:

--  The above copyright notice and this permission notice shall be included in
--  all copies or substantial portions of the Software.

--  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
--  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
--  THE SOFTWARE.

--  NB: this is the MIT License, as found 12-Sep-2007 on the site
--  http://www.opensource.org/licenses/mit-license.php

with Match_3;

with Gnoga.Application.Multi_Connect;
with Gnoga.Gui.Window;
with Gnoga.Gui.Base;
with Gnoga.Gui.Element;
--  with Gnoga.Gui.Element.Common;
with Gnoga.Gui.Element.Canvas;
with Gnoga.Gui.Plugin.Pixi;
with Gnoga.Gui.Plugin.Pixi.Graphics;
with Gnoga.Gui.Plugin.Pixi.Sprite;
with Gnoga.Gui.View;
with Gnoga.Types;

with Ada.Numerics.Float_Random;

with UXStrings.Conversions;

procedure Leaves_main is
   use Match_3;
   use Gnoga;
   use Gnoga.Types;
   use Gnoga.Gui;
   use Gnoga.Gui.Element;
   use Gnoga.Gui.Element.Canvas;
   use Gnoga.Gui.Plugin.Pixi;
   use Gnoga.Gui.Plugin.Pixi.Sprite;
   use Gnoga.Gui.View;

   use all type Gnoga.String;

   function Bitmap
     (c : Cell)
      return Gnoga.String
   is
   begin
      case c is
         when empty =>
            return "";
         when tile_1 =>
            return "d_0536.png";
         when tile_2 =>
            return "d_0537.png";
         when tile_3 =>
            return "d_0544.png";
         when tile_4 =>
            return "d_0541.png";
         when tile_5 =>
            return "d_0540.png";
         when tile_6 =>
            return "d_0542.png";
         when tile_7 =>
            return "d_0543.png";
         when tile_8 =>
            return "d_0539.png";
         when tile_9 =>
            return "d_0545.png";
         when tile_10 =>
            return "d_0546.png";
      end case;
   end Bitmap;

   type Board_Sprite is array (Positive range <>, Positive range <>) of Sprite_Type;

   type Game_App_Data (columns, rows : Positive) is new Connection_Data_Type with record
      Main_Window    : Window.Pointer_To_Window_Class;
      Background     : View_Type;
      My_Canvas      : aliased Canvas.Canvas_Type;
      A              : Plugin.Pixi.Application_Type;
      C              : Plugin.Pixi.Container_Type;
      R              : Plugin.Pixi.Renderer_Type;
      My_Board       : Match_3.Board (1 .. columns, 1 .. rows);
      Sprite         : Board_Sprite (1 .. columns, 1 .. rows);
      Bkg_pic        : Sprite_Type;  --  Background image
      Tile_choice    : Sprite_Type;  --  Highlighting the chosen tile
      Btn_Down_pix_x : Natural;
      Btn_Down_pix_y : Natural;
      Btn_Down_pos   : Cell_pos;
      Btn_Up_pos     : Cell_pos;
      Click_1_pos    : Cell_pos;
      Click_2_pos    : Cell_pos;
   end record;
   type App_Access is access all Game_App_Data;

   procedure Unclick (App : in out Game_App_Data) is
   begin
      App.Click_2_pos  := null_cell_pos;
      App.Btn_Down_pos := null_cell_pos;
   end Unclick;

   cell_size        : constant := 68;
   cell_padding     : constant := 1;
   grid_width       : constant := 2;
   cell_size_padded : constant := cell_size + cell_padding * 2 + grid_width;

   function Pix_hor_pos
     (cp : Cell_pos)
      return Integer
   is
   begin
      return grid_width + (cp.x - 1) * cell_size_padded;
   end Pix_hor_pos;

   function Pix_ver_pos
     (cp : Cell_pos)
      return Integer
   is
   begin
      return grid_width + (cp.y - 1) * cell_size_padded;
   end Pix_ver_pos;

   procedure Create_tile_sprite
     (App : in out Game_App_Data;
      cp  :        Cell_pos)
   is
      cl : constant Cell := App.My_Board (cp.x, cp.y);
   begin
      if cl = empty then
         return;
      end if;
      App.Sprite (cp.x, cp.y).Create (App.C, "img/" & Bitmap (cl), Column => Pix_hor_pos (cp), Row => Pix_ver_pos (cp));
   end Create_tile_sprite;

   trace : constant Boolean := True;

   procedure Game_log (s : Gnoga.String) is
   begin
      if trace then
         Gnoga.Log (s);
      end if;
   end Game_log;

   procedure Swap_tiles
     (App    : in out Game_App_Data;
      c1, c2 :        Cell_pos)
   is
      swap_time          : constant               := 0.2;
      dx                 : constant Integer       := cell_size_padded * (c2.x - c1.x);
      dy                 : constant Integer       := cell_size_padded * (c2.y - c1.y);
      vx                 : constant Velocity_Type := Velocity_Type (dx) / swap_time;
      vy                 : constant Velocity_Type := Velocity_Type (dy) / swap_time;
      ox1, oy1, ox2, oy2 : Integer;
      aux                : Cell;
   begin
      Game_log ("Swapping: (" & Image (c1.x) & Image (c1.y) & ") and (" & Image (c2.x) & Image (c2.y) & ")");
      Unclick (App);
      App.Sprite (c1.x, c1.y).Position (ox1, oy1);
      App.Sprite (c2.x, c2.y).Position (ox2, oy2);
      App.Sprite (c1.x, c1.y).Motion (Column_Velocity => +vx, Row_Velocity => +vy);
      App.Sprite (c2.x, c2.y).Motion (Column_Velocity => -vx, Row_Velocity => -vy);
      delay swap_time;
      --  Stop movement
      App.Sprite (c1.x, c1.y).Motion (0.0, 0.0);
      App.Sprite (c2.x, c2.y).Motion (0.0, 0.0);
      --  Set new exact position (sometimes the animation's displacement doesn't match time * speed)
      App.Sprite (c1.x, c1.y).Locate (ox2, oy2);
      App.Sprite (c2.x, c2.y).Locate (ox1, oy1);
      --  Swap the board's contents
      aux                       := App.My_Board (c1.x, c1.y);
      App.My_Board (c1.x, c1.y) := App.My_Board (c2.x, c2.y);
      App.My_Board (c2.x, c2.y) := aux;
      --  Swap the sprites themselves (Delete & Re-Create)
      App.Sprite (c1.x, c1.y).Finalize;
      App.Sprite (c2.x, c2.y).Finalize;
      Create_tile_sprite (App, (c1.x, c1.y));
      Create_tile_sprite (App, (c2.x, c2.y));
   end Swap_tiles;

   level_probs : constant Tile_prob := (tile_1 .. tile_6 => 1.0 / 6.0, others => 0.0);

   --  Process_matches: show matches, empty the cells, count points, let new tiles down
   procedure Process_matches (App : in out Game_App_Data) is
      ma : constant Match_list := Matches (App.My_Board);
      use Ada.Numerics.Float_Random;
      gen                : Generator;
      animation_slowdown : constant := 1;
      --
      procedure Show_matches is
         fx : array (ma'Range) of Sprite_Type;
         function Image is new UXStrings.Conversions.Scalar_Image (Match_kind);
      begin
         for tch in ma'Range loop
            fx (tch).Create
              (App.C, "img/" & To_Lower (Image (ma (tch).kind)) & ".png", Column => Pix_hor_pos (ma (tch).pos),
               Row => Pix_ver_pos (ma (tch).pos), Column_Velocity => 6.0 * (Random (gen) - 0.5),
               Row_Velocity                                                      => Random (gen) - 4.0);
         end loop;
         Game_log ("[--Delay--] Show matches");
         delay 0.41;
         for tch in ma'Range loop
            fx (tch).Finalize;
         end loop;
      end Show_matches;
      --
      procedure Delete_matches is
      begin
         Empty_matching_cells (App.My_Board, ma);
         for x in 1 .. App.columns loop
            for y in 1 .. App.rows loop
               if App.My_Board (x, y) = empty then
                  App.Sprite (x, y).Finalize;
               end if;
            end loop;
         end loop;
         delay 0.11 * animation_slowdown;
      end Delete_matches;
      --
      procedure New_tiles is
         no_more_move : Boolean;
         procedure Visual_gravity_step is
            gl                        : constant Gravity_list := Gravity_step (App.My_Board, level_probs);
            dx, dy, dst, dmax, nx, ny : Float;
            vx, vy                    : Velocity_Type;
            fall_time_one_cell        : constant              := 0.19 * animation_slowdown;
            stopped                   : Natural               := 0;
         begin
            no_more_move := gl'Length = 0;
            if no_more_move then
               return;
            end if;
            Apply_gravity_moves (App.My_Board, gl);
            --  App.R.Auto_Rendering (App.C, False);
            dmax := 0.0;
            for g of gl loop
               if g.from.y > 0 then  --  On the board
                  App.Sprite (g.from.x, g.from.y).Finalize;
               end if;
               Create_tile_sprite (App, (g.to.x, g.to.y));
               App.Sprite (g.to.x, g.to.y).Locate (Row => Pix_ver_pos (g.from), Column => Pix_hor_pos (g.from));
               dx   := Float (Pix_hor_pos (g.to) - Pix_hor_pos (g.from));
               dy   := Float (Pix_ver_pos (g.to) - Pix_ver_pos (g.from));
               dst  := Float'Max (abs dx, abs dy);  --  Actually always = dy
               dmax := Float'Max (dmax, dst);
               if dst < 1.0 then
                  nx := 0.0;
                  ny := 0.0;
               else
                  nx := dx * Float (cell_size_padded) / dst;
                  ny := dy * Float (cell_size_padded) / dst;
               end if;
               vx := nx / fall_time_one_cell;
               vy := ny / fall_time_one_cell;
               App.Sprite (g.to.x, g.to.y).Motion (Column_Velocity => vx, Row_Velocity => vy);
            end loop;
            --  App.R.Auto_Rendering (App.C, True);
            Game_log ("[--Delays--] Falling tiles");
            multi_tile :
            for tile_step in 1 .. App.My_Board'Length (2) loop
               delay fall_time_one_cell;
               for g of gl loop
                  if g.to.y - g.from.y = tile_step then
                     --  Stop movement
                     App.Sprite (g.to.x, g.to.y).Motion (0.0, 0.0);
                     --  Set new exact position (sometimes the animation's displacement unaccuracy)
                     App.Sprite (g.to.x, g.to.y).Locate (Row => Pix_ver_pos (g.to), Column => Pix_hor_pos (g.to));
                     stopped := stopped + 1;
                     exit multi_tile when stopped = gl'Length;
                  end if;
               end loop;
            end loop multi_tile;
         end Visual_gravity_step;
      begin
         loop
            Visual_gravity_step;
            exit when no_more_move;
         end loop;
      end New_tiles;
   begin
      Reset (gen);
      Show_matches;
      Delete_matches;
      New_tiles;
   end Process_matches;

   procedure Mouse_Down
     (Object      : in out Gnoga.Gui.Base.Base_Type'Class;
      Mouse_Event : in     Gnoga.Gui.Base.Mouse_Event_Record);

   procedure Check_match
     (App            : in out Game_App_Data;
      swap_1, swap_2 :        Cell_pos)
   is
   begin
      App.My_Canvas.On_Mouse_Down_Handler (null);
      --  Un-display choice highlighter
      App.Tile_choice.Finalize;
      Game_log
        ("Trying to swap: (" & Image (swap_1.x) & Image (swap_1.y) & ") and (" & Image (swap_2.x) & Image (swap_2.y) &
         ")");
      if swap_1.x in App.My_Board'Range (1) and then swap_1.y in App.My_Board'Range (2)
        and then swap_2.x in App.My_Board'Range (1) and then swap_2.y in App.My_Board'Range (2)
        and then App.My_Board (swap_1.x, swap_1.y) in Movable_tile
        and then App.My_Board (swap_2.x, swap_2.y) in Movable_tile
        and then
        ((abs (swap_1.x - swap_2.x) = 1 and swap_1.y = swap_2.y) or
         (abs (swap_1.y - swap_2.y) = 1 and swap_1.x = swap_2.x))
      then
         Swap_tiles (App, swap_1, swap_2);
         if Find_any_match (App.My_Board) then
            --  Possible chain reaction: when gaps left from previous matches
            --  are filled, other matches may occur. Hence the following loop.
            loop
               Process_matches (App);
               exit when not Find_any_match (App.My_Board);
            end loop;
         else
            Swap_tiles (App, swap_1, swap_2);  --  No match, swap the tiles back.
         end if;
      end if;
      App.My_Canvas.On_Mouse_Down_Handler (Mouse_Down'Unrestricted_Access);
   end Check_match;

   procedure On_Connect
     (Main_Window : in out Gnoga.Gui.Window.Window_Type'Class;
      Connection  :        access Gnoga.Application.Multi_Connect.Connection_Holder_Type);

   procedure Mouse_Move
     (Object      : in out Gnoga.Gui.Base.Base_Type'Class;
      Mouse_Event : in     Gnoga.Gui.Base.Mouse_Event_Record);
   procedure Mouse_Up
     (Object      : in out Gnoga.Gui.Base.Base_Type'Class;
      Mouse_Event : in     Gnoga.Gui.Base.Mouse_Event_Record);

   procedure On_Connect
     (Main_Window : in out Gnoga.Gui.Window.Window_Type'Class;
      Connection  :        access Gnoga.Application.Multi_Connect.Connection_Holder_Type)
   is
      pragma Unreferenced (Connection);
      App : constant App_Access := new Game_App_Data (13, 7);
      G   : Plugin.Pixi.Graphics.Graphics_Type;
   begin
      Main_Window.Connection_Data (App);

      App.Main_Window := Main_Window'Unchecked_Access;

      --  App.Background.Create (Main_Window);
      --  App.Background.Background_Color ("orange");

      Plugin.Pixi.Load_PIXI (Main_Window);

      App.My_Canvas.Create
        (App.Main_Window.all, grid_width + App.My_Board'Length (1) * cell_size_padded,
         grid_width + App.My_Board'Length (2) * cell_size_padded);
      App.My_Canvas.Border;
      App.My_Canvas.Place_Inside_Bottom_Of (App.Main_Window.Document.Body_Element.all);
      App.My_Canvas.On_Mouse_Down_Handler (Mouse_Down'Unrestricted_Access);

      Fill (App.My_Board, level_probs);
      Unclick (App.all);

      App.A.Create
        (App.My_Canvas, grid_width + App.My_Board'Length (1) * cell_size_padded,
         grid_width + App.My_Board'Length (2) * cell_size_padded);
      App.R.Create (App.A);
      App.C.Create (App.A);
      G.Create (App.C);

      --  G.Fill_Color ("yellow");
      --  G.Fill_Rectangle ((0, 0, App.My_Canvas.Width, App.My_Canvas.Height));
      --  R.Render (App.C);

--        C.Font (Height => "40px");
--        G.Stroke_Text (R, "Hello World!", 100, 100);

      App.Bkg_pic.Create (App.C, "img/board_bkg_1.jpg", 0, 0);
      for x in App.My_Board'Range (1) loop
         for y in App.My_Board'Range (2) loop
            Create_tile_sprite (App.all, (x, y));
         end loop;
      end loop;
      App.R.Auto_Rendering (App.C, True);

--        loop
--           delay 1.0;
--        end loop;
--  XX on loop exit XX     Gnoga.Gui.Plugin.Pixi.Sprite.Delete_All (App.C);
--  XX on loop exit XX      App.R.Auto_Rendering (App.C, False);

--  XX on loop exit XX      Connection.Hold;
   end On_Connect;

   function Locate_cell
     (mx, my : Natural)
      return Cell_pos
   is
   begin
      return (1 + mx / cell_size_padded, 1 + my / cell_size_padded);
   end Locate_cell;

   procedure Mouse_Down
     (Object      : in out Gnoga.Gui.Base.Base_Type'Class;
      Mouse_Event : in     Gnoga.Gui.Base.Mouse_Event_Record)
   is
      App : constant App_Access := App_Access (Object.Connection_Data);
   begin
      App.Btn_Down_pix_x := Mouse_Event.X;
      App.Btn_Down_pix_y := Mouse_Event.Y;
      App.Btn_Down_pos   := Locate_cell (App.Btn_Down_pix_x, App.Btn_Down_pix_y);
      --  Un-display previous choice highlighter
      App.Tile_choice.Finalize;
      --  Highlight new choice
      if App.Btn_Down_pos.x in App.My_Board'Range (1) and then App.Btn_Down_pos.y in App.My_Board'Range (2) then
         App.Tile_choice.Create
           (App.C, "img/select.png", Column => Pix_hor_pos (App.Btn_Down_pos) - 2,
            Row                             => Pix_ver_pos (App.Btn_Down_pos) - 2);
      else
         App.Btn_Down_pos := null_cell_pos;
      end if;
      Game_log ("Mouse Down; Down pos =" & Image (App.Btn_Down_pix_x) & Image (App.Btn_Down_pix_y));
      App.My_Canvas.On_Mouse_Move_Handler (Mouse_Move'Unrestricted_Access);
      App.My_Canvas.On_Mouse_Up_Handler (Mouse_Up'Unrestricted_Access);
   end Mouse_Down;

   procedure Mouse_Move
     (Object      : in out Gnoga.Gui.Base.Base_Type'Class;
      Mouse_Event : in     Gnoga.Gui.Base.Mouse_Event_Record)
   is
      App         : constant App_Access := App_Access (Object.Connection_Data);
      dx, dy      : Integer;
      target_cell : Cell_pos            := Locate_cell (Mouse_Event.X, Mouse_Event.Y);
      slide       : Boolean             := False;
   begin
      Game_log ("Mouse Move" & Image (Mouse_Event.X) & Image (Mouse_Event.Y));
      if App.Btn_Down_pos = null_cell_pos then
         return;
      end if;
      dx := Mouse_Event.X - App.Btn_Down_pix_x;
      dy := Mouse_Event.Y - App.Btn_Down_pix_y;
      if abs dx > cell_size / 2 then
         Game_log ("Hor. Slide, delta= " & Image (dx));
         slide := True;
         if dx < 0 then
            target_cell := (Integer'Max (1, App.Btn_Down_pos.x - 1), App.Btn_Down_pos.y);
         else
            target_cell := (Integer'Min (App.columns, App.Btn_Down_pos.x + 1), App.Btn_Down_pos.y);
         end if;
      elsif abs dy > cell_size / 2 then
         Game_log ("Ver. Slide, delta= " & Image (dy));
         slide := True;
         if dy < 0 then
            target_cell := (App.Btn_Down_pos.x, Integer'Max (1, App.Btn_Down_pos.y - 1));
         else
            target_cell := (App.Btn_Down_pos.x, Integer'Min (App.rows, App.Btn_Down_pos.y + 1));
         end if;
      end if;
      if slide then
         App.My_Canvas.On_Mouse_Move_Handler (null);
         Check_match (App.all, App.Btn_Down_pos, target_cell);
      end if;
   end Mouse_Move;

   procedure Mouse_Up
     (Object      : in out Gnoga.Gui.Base.Base_Type'Class;
      Mouse_Event : in     Gnoga.Gui.Base.Mouse_Event_Record)
   is
      App : constant App_Access := App_Access (Object.Connection_Data);
   begin
      Game_log ("Mouse Up" & Image (Mouse_Event.X) & Image (Mouse_Event.Y));
      App.My_Canvas.On_Mouse_Move_Handler (null);
      App.My_Canvas.On_Mouse_Up_Handler (null);
      App.Btn_Up_pos := Locate_cell (Mouse_Event.X, Mouse_Event.Y);
      if App.Btn_Down_pos = App.Btn_Up_pos then
         --  It's a click (down then up in the same cell),
         --  even a slow click or with some movement between down and up.
         Game_log ("Click");
         App.Click_1_pos := App.Click_2_pos;
         App.Click_2_pos := App.Btn_Up_pos;
         if not (App.Click_2_pos.x in App.My_Board'Range (1) and then App.Click_2_pos.y in App.My_Board'Range (2)) then
            App.Click_2_pos := null_cell_pos;
         end if;
         if App.Click_1_pos = App.Click_2_pos then
            --  Two clicks in the same cell: do nothing
            null;
         elsif App.Click_1_pos /= null_cell_pos then
            Check_match (App.all, App.Click_1_pos, App.Click_2_pos);
         end if;
      end if;
   end Mouse_Up;

begin
   Application.Multi_Connect.Initialize (Event => On_Connect'Unrestricted_Access, Boot => "debug.html");

   Application.Title ("Plant mania");
   Application.HTML_On_Close ("<b>Connection to Application has been terminated</b>");

   Application.Multi_Connect.Message_Loop;
end Leaves_main;