-
Notifications
You must be signed in to change notification settings - Fork 0
/
uspacecommand.pas
700 lines (640 loc) · 17.8 KB
/
uspacecommand.pas
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
(******************************************************************************)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* This file is part of Greenfoot for Lazarus *)
(* *)
(* See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(******************************************************************************)
(*
* made by Corpsman, www.Corpsman.de
*
* Version : 0.01
* History : 0.01 = Initial version ( TBigRock, TRock, TEnemy )
*
* Todo : New Weapons for the player..
*)
Unit uspacecommand;
{$MODE objfpc}{$H+}
Interface
Uses
SysUtils,
math,
ugreenfoot,
uvectormath,
OpenGLContext;
Const
corners = 10;
Type
{ TCounter }
TCounter = Class(TActor)
private
text: String;
StringLength: integer;
Procedure UpdateImage();
public
value: integer;
Constructor Create(); override;
Constructor Create(prefix: String);
Destructor destroy(); override;
Procedure Increment(Value_: integer);
End;
{ TEnemy }
TEnemy = Class(TActor)
private
goal: TActor;
pos, speed: TVector2;
worldwidth, worldheight: integer;
GunLoadTimer: Integer;
public
Constructor create(); override;
Procedure addedToWorld(Const World: TWorld); override;
Procedure Act(); override;
End;
{ TBigRock }
TBigRock = Class(TActor)
private
speed, pos: TVector2;
worldwidth, worldheight: integer;
public
Constructor create(); override;
Procedure addedToWorld(Const World: TWorld); override;
Procedure Act(); override;
End;
{ TRock }
TRock = Class(TBigRock)
public
Constructor create(); override;
End;
{ TSpace }
TSpace = Class(Tworld)
private
level: integer;
counter: TCounter;
public
ShootsInGame: Integer;
Constructor create(Parent: TOpenGLControl);
Procedure CreateLevel;
Procedure EndGame();
Procedure Count(Points: integer);
Procedure Act(); override;
End;
{ TSpaceGunShoot }
TSpaceGunShoot = Class(TActor)
private
pos: TVector2;
speed: TVector2;
lifetime: integer;
worldwidth, worldheight: integer;
public
Constructor create(); override;
Procedure addedToWorld(Const World: TWorld); override;
Procedure act; override;
End;
{ TEnemyGunShoot }
TEnemyGunShoot = Class(TSpaceGunShoot)
private
Owner_: TActor;
public
Constructor create(); override;
Procedure addedToWorld(Const World: TWorld); override;
Procedure act; override;
End;
{ TSpaceShip }
TSpaceShip = Class(TActor)
private
shoots, GunReloadTimer, GunLoadTimer: Integer;
blocker: boolean;
img_boost, img_normal: TGreenfootImage;
speed: TVector2;
pos: TVector2;
maxspeed: integer;
worldwidth, worldheight: integer;
public
Constructor create(); override;
Procedure addedToWorld(Const World: TWorld); override;
Procedure Act(); override;
End;
Implementation
{ TEnemyGunShoot }
Constructor TEnemyGunShoot.create;
Var
img: TGreenfootImage;
Begin
Inherited create;
img := GreenFootGraphicEngine.FindImage('SpaceGunEnemyShoot');
If Not assigned(img) Then Begin
img := TGreenfootImage.Create(2, 16);
img.BeginUpdate();
img.SetColor(magenta);
img.fillRect(0, 0, 2, 16);
img.EndUpdate();
GreenFootGraphicEngine.AddImage(img, 'SpaceGunEnemyShoot');
End;
setImage(img);
End;
Procedure TEnemyGunShoot.addedToWorld(Const World: TWorld);
Var
dir: integer;
spaceship: TEnemy;
Begin
spaceship := TEnemy(Owner_);
setRotation(spaceship.getRotation());
pos := v2(getx, gety);
dir := getRotation() + 90;
speed := spaceship.speed;
speed.x := speed.x + cos(degtorad(dir)) * 10;
speed.y := speed.y + sin(degtorad(dir)) * 10;
worldwidth := World.getWidth();
worldheight := World.getHeight();
End;
Procedure TEnemyGunShoot.act;
Var
rock: TActor;
Begin
dec(lifetime);
If lifetime > 0 Then Begin
pos := pos + speed;
setLocation(mod2(round(pos.x), worldwidth), mod2(round(pos.y), worldheight), False);
// Alles was wir treffen können
rock := getOneIntersectingObject(TSpaceShip);
If assigned(rock) Then Begin
getWorld().removeObject(self);
getWorld().removeObject(rock);
TSpace(Getworld()).EndGame();
End;
End
Else Begin
getWorld().removeObject(self);
End;
End;
{ TCounter }
Constructor TCounter.Create;
Begin
create('');
End;
Constructor TCounter.Create(prefix: String);
Var
image: TGreenfootImage;
Begin
Inherited Create;
text := prefix;
StringLength := (length(text) + 2) * 10;
image := TGreenfootImage.Create(StringLength, 16);
setimage(image);
//image.free;
updateImage();
End;
Destructor TCounter.destroy;
Begin
getimage().free;
Inherited destroy;
End;
Procedure TCounter.Increment(Value_: integer);
Begin
value := value + Value_;
UpdateImage();
End;
Procedure TCounter.UpdateImage;
Var
image: TGreenfootImage;
Begin
image := getimage();
image.clear();
image.SetColor(white);
image.drawString(text + inttostr(value), 1, 12);
End;
{ TEnemy }
Constructor TEnemy.create;
Var
img: TGreenfootImage;
Begin
Inherited create;
img := GreenFootGraphicEngine.FindImage('SpEnemy');
If Not assigned(img) Then Begin
img := TGreenfootImage.Create(30, 30);
img.BeginUpdate();
img.clear();
img.SetColor(magenta);
img.drawPolygon([5, 25, 15], [5, 5, 25], 3);
img.drawLine(5, 5, 5, 15);
img.drawLine(25, 5, 25, 15);
img.EndUpdate();
GreenFootGraphicEngine.AddImage(img, 'SpEnemy');
End;
setImage(img);
GunLoadTimer := 50 + random(35); // Sonst schießen alle Gegner immer Gleichzeitig
End;
Procedure TEnemy.addedToWorld(Const World: TWorld);
Begin
goal := world.getOneObject(TSpaceShip);
pos := v2(getx, gety);
speed := v2((random(10) - 5) / 2.5, (random(10) - 5) / 2.5);
worldheight := world.getHeight();
worldwidth := world.getWidth();
End;
Procedure TEnemy.Act;
Var
dir, dx, dy: integer;
shoot: TEnemyGunShoot;
Begin
pos := pos + speed;
setLocation(
Mod2(round(pos.x), worldwidth),
Mod2(round(pos.y), worldheight)
, false);
dx := getx - goal.getx;
dy := gety - goal.gety;
dir := round(radtodeg(arctan2(dy, dx))) + 90;
setRotation(dir);
If GunLoadTimer = 0 Then Begin
GunLoadTimer := 50 + random(15);
dir := -dx;
dx := dy;
dy := dir;
dir := dx * dx + dy * dy;
dx := round(10 * dx / sqrt(dir));
dy := round(10 * dy / sqrt(dir));
shoot := TEnemyGunShoot.create();
shoot.Owner_ := self;
getworld().addObject(shoot, getx + dx, gety + dy);
shoot := TEnemyGunShoot.create();
shoot.Owner_ := self;
getworld().addObject(shoot, getx - dx, gety - dy);
End;
GunLoadTimer := max(0, GunLoadTimer - 1);
End;
{ TRock }
Constructor TRock.create;
Var
img: TGreenfootImage;
delta, r: single;
xa, ya, x, y, ox, oy, i: integer;
num: integer;
Begin
Inherited create;
num := random(10);
img := GreenFootGraphicEngine.FindImage('Rock' + inttostr(num));
If Not assigned(img) Then Begin
img := TGreenfootImage.Create(25, 25);
img.BeginUpdate();
img.clear();
delta := 360 / corners;
r := random(10) - 5 + img.GetWidth Div 3;
i := 0;
xa := round(img.GetWidth / 2 + cos(degtorad(i * delta)) * r);
ya := round(img.GetHeight / 2 + sin(degtorad(i * delta)) * r);
ox := xa;
oy := ya;
img.SetColor(gray);
For i := 1 To 9 Do Begin
r := random(10) - 5 + img.GetWidth Div 3;
x := round(img.GetWidth / 2 + cos(degtorad(i * delta)) * r);
y := round(img.GetHeight / 2 + sin(degtorad(i * delta)) * r);
img.drawLine(ox, oy, x, y);
ox := x;
oy := y;
End;
img.drawLine(ox, oy, xa, ya);
img.EndUpdate();
GreenFootGraphicEngine.AddImage(img, 'Rock' + inttostr(num));
End;
setImage(img);
End;
{ TBigRock }
Constructor TBigRock.create;
Var
img: TGreenfootImage;
delta, r: single;
xa, ya, x, y, ox, oy, i: integer;
num: integer;
Begin
Inherited create;
num := random(10);
img := GreenFootGraphicEngine.FindImage('BigRock' + inttostr(num));
If Not assigned(img) Then Begin
img := TGreenfootImage.Create(50, 50);
img.BeginUpdate();
img.clear();
delta := 360 / corners;
r := random(10) - 5 + img.GetWidth Div 3;
i := 0;
xa := round(img.GetWidth / 2 + cos(degtorad(i * delta)) * r);
ya := round(img.GetHeight / 2 + sin(degtorad(i * delta)) * r);
ox := xa;
oy := ya;
img.SetColor(gray);
For i := 1 To 9 Do Begin
r := random(10) - 5 + img.GetWidth Div 3;
x := round(img.GetWidth / 2 + cos(degtorad(i * delta)) * r);
y := round(img.GetHeight / 2 + sin(degtorad(i * delta)) * r);
img.drawLine(ox, oy, x, y);
ox := x;
oy := y;
End;
img.drawLine(ox, oy, xa, ya);
img.EndUpdate();
GreenFootGraphicEngine.AddImage(img, 'BigRock' + inttostr(num));
End;
setImage(img);
End;
Procedure TBigRock.addedToWorld(Const World: TWorld);
Begin
pos := v2(getx, gety);
speed := v2((random(10) - 5) / 2.5, (random(10) - 5) / 2.5);
worldheight := world.getHeight();
worldwidth := world.getWidth();
End;
Procedure TBigRock.Act;
Begin
pos := pos + speed;
setLocation(
Mod2(round(pos.x), worldwidth),
Mod2(round(pos.y), worldheight)
, false);
End;
{ TSpaceGunShoot }
Constructor TSpaceGunShoot.create;
Var
img: TGreenfootImage;
Begin
Inherited create;
img := GreenFootGraphicEngine.FindImage('SpaceGunShoot');
If Not assigned(img) Then Begin
img := TGreenfootImage.Create(2, 16);
img.BeginUpdate();
img.SetColor(yellow);
img.fillRect(0, 0, 2, 16);
img.EndUpdate();
GreenFootGraphicEngine.AddImage(img, 'SpaceGunShoot');
End;
setImage(img);
lifetime := 50;
End;
Procedure TSpaceGunShoot.addedToWorld(Const World: TWorld);
Var
dir: integer;
spaceship: TSpaceShip;
Begin
spaceship := TSpaceShip(world.getOneObject(TSpaceShip)); // Das Parent Hohlen
setRotation(spaceship.getRotation());
pos := v2(getx, gety);
dir := getRotation() + 90;
speed := spaceship.speed;
speed.x := speed.x + cos(degtorad(dir)) * 10;
speed.y := speed.y + sin(degtorad(dir)) * 10;
worldwidth := World.getWidth();
worldheight := World.getHeight();
End;
Procedure TSpaceGunShoot.act;
Var
rock: TActor;
x, y: integer;
Begin
dec(lifetime);
If lifetime > 0 Then Begin
pos := pos + speed;
setLocation(mod2(round(pos.x), worldwidth), mod2(round(pos.y), worldheight), False);
// Alles was wir treffen können
rock := getOneIntersectingObject(TBigRock);
If assigned(rock) Then Begin
TSpace(getWorld()).ShootsInGame := TSpace(getWorld()).ShootsInGame - 1;
getWorld().removeObject(self);
x := rock.getx;
y := rock.gety;
getWorld().removeObject(rock);
getWorld().addObject(TRock.create(), x, y);
getWorld().addObject(TRock.create(), x, y);
getWorld().addObject(TRock.create(), x, y);
getWorld().addObject(TRock.create(), x, y);
TSpace(Getworld()).Count(10);
End;
rock := getOneIntersectingObject(TRock);
If assigned(rock) Then Begin
TSpace(getWorld()).ShootsInGame := TSpace(getWorld()).ShootsInGame - 1;
getWorld().removeObject(self);
getWorld().removeObject(rock);
TSpace(Getworld()).Count(15);
End;
rock := getOneIntersectingObject(TEnemy);
If assigned(rock) Then Begin
TSpace(getWorld()).ShootsInGame := TSpace(getWorld()).ShootsInGame - 1;
getWorld().removeObject(self);
getWorld().removeObject(rock);
TSpace(Getworld()).Count(30);
End;
End
Else Begin
TSpace(getWorld()).ShootsInGame := TSpace(getWorld()).ShootsInGame - 1;
getWorld().removeObject(self);
End;
End;
{ TSpaceShip }
Constructor TSpaceShip.create;
Begin
Inherited create;
shoots := 0;
blocker := false;
GunLoadTimer := 0;
maxspeed := 15;
img_normal := GreenFootGraphicEngine.FindImage('img_normal');
If Not assigned(img_normal) Then Begin
img_normal := TGreenfootImage.Create(30, 40);
img_normal.BeginUpdate();
img_normal.clear();
img_normal.SetColor(RED);
// Antialiased Spaceship
img_normal.drawPolygon([5, 15, 25, 15], [5, 15, 5, 25], 4);
img_normal.drawPolygon([4, 15, 26, 15], [4, 16, 4, 26], 4);
img_normal.EndUpdate();
GreenFootGraphicEngine.AddImage(img_normal, 'img_normal');
End;
setImage(img_normal);
// Draw the Boost
img_boost := GreenFootGraphicEngine.FindImage('img_boost ');
If Not assigned(img_boost) Then Begin
img_boost := TGreenfootImage.Create(30, 40);
img_boost.BeginUpdate();
img_boost.clear();
img_boost.drawImage(img_normal, 0, 0);
img_boost.SetColor(yellow);
img_boost.drawLine(10, 10, 8, 2);
img_boost.drawLine(20, 10, 22, 2);
img_boost.EndUpdate();
GreenFootGraphicEngine.AddImage(img_boost, 'img_boost');
End;
End;
Procedure TSpaceShip.addedToWorld(Const World: TWorld);
Begin
worldheight := world.getHeight();
worldwidth := world.getWidth();
pos := v2(getx, gety);
speed := v2(0, 0);
End;
Procedure TSpaceShip.Act;
Const
Thrust = 0.5;
ShootThrust = 0.5;
Var
x, y, dir: integer;
gunshot: TSpaceGunShoot;
Begin
// Move
If LenV2sqr(speed) >= sqr(ShootThrust) Then Begin
pos := pos + speed;
setLocation(Mod2(round(pos.x), worldwidth), mod2(round(pos.y), worldheight), false);
End
Else Begin
speed := v2(0, 0);
End;
If isKeyDown(key_left) Then Begin
setRotation(getRotation() - 5);
End;
If isKeyDown(key_right) Then Begin
setRotation(getRotation() + 5);
End;
If isKeyDown(key_up) Then Begin
dir := getRotation() + 90;
speed.x := speed.x + (cos(degtorad(dir)) * Thrust);
speed.y := speed.y + (sin(degtorad(dir)) * Thrust);
If LenV2SQR(speed) > sqr(maxspeed) Then Begin
speed := ScaleV2(maxspeed / LenV2(speed), speed);
End;
setImage(img_boost);
End
Else Begin
// speed := ScaleV2(0.99, speed); // To enable friction
setImage(img_normal);
End;
//If isKeyDown(key_space) And (GunLoadTimer = 0) And (Not blocker) Then Begin // More Realistik
If isKeyDown(key_space) And (TSpace(getWorld()).ShootsInGame < 10) And (GunLoadTimer = 0) Then Begin // Better for playing
TSpace(getWorld()).ShootsInGame := TSpace(getWorld()).ShootsInGame + 1;
dir := getRotation() + 90;
speed.x := speed.x - (cos(degtorad(dir)) * ShootThrust);
speed.y := speed.y - (sin(degtorad(dir)) * ShootThrust);
GunReloadTimer := 35;
GunLoadTimer := 5; // Die Zeit die es Dauert bis wieder geschossen werden kann
inc(shoots);
If Shoots >= 10 Then Begin
Blocker := true;
End;
dir := getRotation() + 90;
x := round(getx + (cos(degtorad(dir)) * 15));
y := round(gety + (sin(degtorad(dir)) * 15));
gunshot := TSpaceGunShoot.create();
getWorld().addObject(gunshot, x, y);
End;
GunLoadTimer := max(0, GunLoadTimer - 1);
GunReloadTimer := max(0, GunReloadTimer - 1);
If (GunReloadTimer = 0) Then Begin
Blocker := false;
shoots := 0;
End;
// Alles was uns Umbringt
If assigned(getOneIntersectingObject(TBigRock)) Then Begin
getworld().removeObject(self);
TSpace(getworld()).EndGame();
End;
If assigned(getOneIntersectingObject(TEnemy)) Then Begin
getworld().removeObject(self);
TSpace(getworld()).EndGame();
End;
If assigned(getOneIntersectingObject(TRock)) Then Begin
getworld().removeObject(self);
TSpace(getworld()).EndGame();
End;
End;
{ TSpace }
Constructor TSpace.create(Parent: TOpenGLControl);
Begin
Inherited create(Parent, 640, 480, 1);
ShootsInGame := 0;
counter := tcounter.create('Points : ');
addObject(counter, 640 - 80, 480 - 35);
level := 0;
addObject(TSpaceShip.create(), 320, 240);
SetSpeed(25);
CreateLevel;
Start();
End;
Procedure TSpace.CreateLevel;
Var
sx, sy, y: integer;
x: integer;
i: integer;
ship: TActor;
Begin
ship := getOneObject(TSpaceShip);
sx := ship.getx;
sy := ship.gety;
For i := 0 To 4 Do Begin
x := Random(640);
y := Random(480);
While (x >= sx - 40) And (x <= sx + 40) Do Begin
x := Random(640);
End;
While (y >= sy - 40) And (y <= sy + 40) Do Begin
y := Random(480);
End;
addObject(TBigRock.create(), x, y);
End;
If level > 0 Then Begin
For i := 0 To level - (level Mod 3) Do Begin
x := Random(640);
y := Random(480);
While (x >= sx - 40) And (x <= sx + 40) Do Begin
x := Random(640);
End;
While (y >= sy - 40) And (y <= sy + 40) Do Begin
y := Random(480);
End;
addObject(TRock.create(), x, y);
End;
End;
If level > 1 Then Begin
// Einfügen von Gegnern
For i := 0 To level - 2 Do Begin
x := Random(640);
y := Random(480);
While (x >= sx - 40) And (x <= sx + 40) Do Begin
x := Random(640);
End;
While (y >= sy - 40) And (y <= sy + 40) Do Begin
y := Random(480);
End;
addObject(TEnemy.create(), x, y);
End;
End;
inc(level);
End;
Procedure TSpace.EndGame;
Begin
stop;
End;
Procedure TSpace.Count(Points: integer);
Begin
counter.Increment(points);
End;
Procedure TSpace.Act;
Var
enemies, rocks, bigrocks: TActor;
Begin
// Wenns nichts mehr zum Abballern gibt
rocks := getOneObject(TRock);
bigrocks := getOneObject(TBigRock);
enemies := getOneObject(TEnemy);
If Not assigned(rocks)
And
Not assigned(bigrocks)
And
Not assigned(enemies) Then Begin
CreateLevel;
End;
End;
End.