behemoth /examples/Shooter/Shooter.cs

Language C# Lines 534
MD5 Hash 328903132493ea1d3c707cc840093700 Estimated Cost $10,106 (why?)
Repository https://github.com/rsaarelm/behemoth.git View Raw File View Project SPDX
  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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;

using Tao.OpenGl;
using Tao.Sdl;

using Behemoth.Util;
using Behemoth.Apps;
using Behemoth.TaoUtil;

namespace Shooter
{
  abstract class Entity
  {
    public virtual void Display(Shooter shooter, int xOffset, int yOffset)
    {
      shooter.DrawSprite((int)X + xOffset, (int)Y + yOffset, Frame);
    }


    public virtual void Update(EntityManager context)
    {
    }


    public void SetHitBox(int x, int y, int w, int h)
    {
      hitBoxX = x;
      hitBoxY = y;
      hitBoxW = w;
      hitBoxH = h;
    }


    public bool Intersects(Entity other)
    {
      return Geom.RectanglesIntersect(
        hitBoxX + X, hitBoxY + Y, hitBoxW, hitBoxH,
        other.hitBoxX + other.X, other.hitBoxY + other.Y, other.hitBoxW, other.hitBoxH);
    }


    public double X;
    public double Y;
    public int Frame;

    private int hitBoxX = 0;
    private int hitBoxY = 0;
    private int hitBoxW = Shooter.spriteWidth;
    private int hitBoxH = Shooter.spriteHeight;
  }


  class Explosion : Entity
  {
    public Explosion(double x, double y)
    {
      X = x;
      Y = y;
      Frame = startFrame;
      cooldown = rate;
    }


    public override void Update(EntityManager context)
    {
      if (cooldown-- <= 0)
      {
        cooldown = rate;
        Frame++;
        if (Frame == endFrame)
        {
          context.Remove(this);
        }
      }
    }


    private const int rate = 4;
    private const int startFrame = 8;
    private const int endFrame = 12;
    private int cooldown;
  }


  class Avatar : Entity
  {
    public Avatar()
    {
      Y = 8.0;
      X = Shooter.pixelWidth / 2 - Shooter.spriteWidth / 2;
      Frame = 16;

      SetHitBox(6, 5, 4, 7);
    }


    public override void Update(EntityManager context)
    {
      if (!IsAlive)
      {
        return;
      }

      if (IsShooting)
      {
        if (cooldown-- <= 0)
        {
          Fire(context);
        }
      }

      if (IsMovingLeft && !IsMovingRight)
      {
        X = Math.Max(minX, X - speed);
      }
      else if (IsMovingRight && !IsMovingLeft)
      {
        X = Math.Min(maxX, X + speed);
      }
    }


    public override void Display(Shooter shooter, int xOff, int yOff)
    {
      if (!IsAlive)
      {
        return;
      }

      base.Display(shooter, xOff, yOff);
    }


    public void Fire(EntityManager context)
    {
      Media.PlaySound(Shooter.playerShotFx);
      context.Add(new AvatarShot(X - 4, Y + 3));
      context.Add(new AvatarShot(X + 5, Y + 3));
      cooldown = firingRate;
    }


    public void Die(EntityManager context)
    {
      if (!IsAlive)
      {
        return;
      }

      Media.PlaySound(Shooter.playerExplodeFx);
      isAlive = false;
      context.StartGameOver();
      context.Add(new Explosion(X, Y));
    }


    public bool IsMovingLeft;

    public bool IsMovingRight;

    public bool IsShooting
    {
      get { return isShooting; }
      set
      {
        isShooting = value;
        cooldown = 0;
      }
    }

    public bool IsAlive { get { return isAlive; } }

    private bool isAlive = true;

    private bool isShooting;

    private int cooldown = 0;

    private const double speed = 4.0;
    private const int firingRate = 2;
    private const double minX = 0.0;
    private const double maxX = Shooter.pixelWidth - Shooter.spriteWidth;
  }


  class AvatarShot : Entity
  {
    public AvatarShot(double x, double y)
    {
      X = x;
      Y = y;
      Frame = 12;
      SetHitBox(6, 6, 3, 4);
    }

    public override void Update(EntityManager context)
    {
      foreach (Entity o in new List<Entity>(context.Entities))
      {
        var enemy = o as Enemy;
        if (enemy != null && this.Intersects(enemy))
        {
          enemy.Hurt(context);
        }
      }

      Y += speed;

      if (Y > Shooter.pixelHeight + Shooter.spriteHeight)
      {
        context.Remove(this);
      }
    }

    private const double speed = 5.0;
  }


  class Enemy : Entity
  {
    public Enemy(double x, double y, double dx, double dy)
    {
      X = x;
      Y = y;
      DX = dx;
      DY = dy;
    }


    public override void Display(Shooter shooter, int xOffset, int yOffset)
    {
      shooter.DrawSprite(
        (int)X + xOffset, (int)Y + yOffset,
        damageBlink > 1 ? blinkFrame : Frame);

      if (damageBlink > 0)
      {
        damageBlink--;
      }
    }


    public override void Update(EntityManager context)
    {
      // Collision detection.

      // XXX: Wasteful iterating through all, could optimize by having
      // collision groups as sublists.

      // XXX: Repeating the iteration pattern from EntityManager...
      foreach (Entity o in new List<Entity>(context.Entities))
      {
        var avatar = o as Avatar;
        if (avatar != null && this.Intersects(avatar))
        {
          avatar.Die(context);
        }
      }

      X += DX;
      Y += DY;

      if (HasLeftStage) {
        context.Remove(this);
      }

      frameCount = (frameCount + 1) % (numFrames * frameDelay);
      Frame = startFrame + (frameCount / frameDelay);
    }


    public void Hurt(EntityManager context)
    {
      if (life-- < 0)
      {
        Die(context);
      }
      if (damageBlink == 0)
      {
        damageBlink = 2;
      }
    }


    public void Die(EntityManager context)
    {
      Media.PlaySound(Shooter.enemyExplodeFx);

      context.Add(new Explosion(X, Y));
      context.Remove(this);
    }


    bool HasLeftStage { get { return Y < -Shooter.spriteHeight; } }


    public double DX;
    public double DY;

    private int frameCount = 0;

    private int life = 10;

    private int damageBlink = 0;

    const int startFrame = 0;
    const int numFrames = 8;
    const int frameDelay = 4;
    const int blinkFrame = 14;
  }


  class EntityManager
  {
    public EntityManager(Shooter shooter)
    {
      ShooterApp = shooter;
    }


    public void Update()
    {
      // Clone the entity list so the update operations can modify the
      // original list without breaking iteration.
      foreach (Entity e in new List<Entity>(Entities))
      {
        e.Update(this);
      }
    }


    public void Display(Shooter shooter, int xOff, int yOff)
    {
      foreach (Entity e in Entities)
      {
        e.Display(shooter, xOff, yOff);
      }
    }


    public void Add(Entity entity)
    {
      Entities.Add(entity);
    }


    public void Remove(Entity entity)
    {
      Entities.Remove(entity);
    }


    public void StartGameOver()
    {
      ShooterApp.StartGameOver();
    }


    public IList<Entity> Entities = new List<Entity>();

    public Shooter ShooterApp;
  }


  public class Shooter : IScreen
  {
    public const int pixelWidth = 240;
    public const int pixelHeight = 320;

    public const int spriteWidth = 16;
    public const int spriteHeight = 16;

    public const string playerShotFx = "pew.wav";
    public const string playerExplodeFx = "player_explode.wav";
    public const string enemyExplodeFx = "enemy_explode.wav";


    const string spriteTexture = "sprites.png";

    EntityManager entities;

    Avatar avatar = new Avatar();

    bool isGameOver = false;

    // How many ticks does the game keep going after game over.
    int gameOverCounter = 40;

    Random rng = new Random();

    List<Vec3> starfield = new List<Vec3>();

    App App;

    public static void Main(string[] args)
    {
      var app = new TaoApp(pixelWidth, pixelHeight, "Behemoth Shooter");
      app.RegisterService(typeof(IScreen), new Shooter());
      app.Run();
    }


    public void Init()
    {
      Media.AddPhysFsPath("Shooter.zip");
      // Make the zip file found from build subdir too, so that it's easy to
      // run the exe from the project root dir.
      Media.AddPhysFsPath("build", "Shooter.zip");

      entities = new EntityManager(this);

      entities.Add(avatar);

      InitStarfield();

      App = App.Instance;
    }


    public void Uninit() {}


    void InitStarfield()
    {
      for (int i = 0; i < 1000; i++)
      {
        starfield.Add(new Vec3(rng.Next(-pixelWidth, pixelWidth), rng.Next(1000), rng.Next(100)));
      }
    }


    public void StartGameOver()
    {
      isGameOver = true;
    }


    public void KeyPressed(int keycode, int keyMod, char ch)
    {
      switch (keycode)
      {
      case Sdl.SDLK_ESCAPE:
        App.Exit();
        break;
      case Sdl.SDLK_LEFT:
        avatar.IsMovingLeft = true;
        break;
      case Sdl.SDLK_RIGHT:
        avatar.IsMovingRight = true;
        break;
      case Sdl.SDLK_SPACE:
        avatar.IsShooting = true;
        break;
      }
    }


    public void KeyReleased(int keycode)
    {
      switch (keycode)
      {
      case Sdl.SDLK_LEFT:
        avatar.IsMovingLeft = false;
        break;
      case Sdl.SDLK_RIGHT:
        avatar.IsMovingRight = false;
        break;
      case Sdl.SDLK_SPACE:
        avatar.IsShooting = false;
        break;
      }
    }


    public void Update(double timeElapsed)
    {
      entities.Update();
      if (isGameOver) {
        if (gameOverCounter-- <= 0) {
          App.Exit();
        }
      }

      if (rng.Next(5) == 0)
      {
        SpawnEnemy();
      }
    }


    public void Draw(double timeElapsed)
    {
      Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
      Gl.glMatrixMode(Gl.GL_MODELVIEW);
      Gl.glLoadIdentity();

      Gfx.DrawStarfield(starfield, TimeUtil.CurrentSeconds * 100,
                        App.Service<ITaoService>().PixelScale,
                        App.Service<ITaoService>().PixelWidth);

      entities.Display(this, 0, 0);

      Sdl.SDL_GL_SwapBuffers();
    }


    public void RandomPoint(out int x, out int y)
    {
      x = rng.Next(0, pixelWidth - spriteWidth);
      y = rng.Next(0, pixelHeight - spriteHeight);
    }


    public void SpawnEnemy()
    {
      double x = pixelWidth / 2 - spriteWidth / 2;
      x += (rng.NextDouble() - 0.5) * pixelWidth * 2.0;

      double dx = 6.0 * (rng.NextDouble() - 0.5);
      entities.Add(new Enemy(x, pixelHeight + spriteHeight, dx, -4.0));
    }


    public void DrawSprite(float x, float y, int frame)
    {
      Gfx.DrawSprite(
        x, y, frame, spriteWidth, spriteHeight,
        App.Service<ITaoService>().Textures[spriteTexture], 8, 8);
    }
  }
}
Back to Top