Browse code

fix collisions

devnewton authored on 12/10/2016 at 21:00:44
Showing 6 changed files
... ...
@@ -1,6 +1,7 @@
1 1
 /// <reference path="../../typings/phaser.d.ts"/>
2
+import {Vulnerable} from "./features/Vulnerable.ts";
2 3
 
3
-export class Bird extends Phaser.Sprite {
4
+export class Bird extends Phaser.Sprite implements Vulnerable {
4 5
 
5 6
     birdExplosion: Phaser.Sprite;
6 7
 
... ...
@@ -19,6 +20,10 @@ export class Bird extends Phaser.Sprite {
19 19
         explodeAnimation.killOnComplete = true;
20 20
     }
21 21
 
22
+    getVulnerableRectangles(): Array<Phaser.Rectangle> {
23
+        return [new Phaser.Rectangle(this.x, this.y, this.width, this.height)];
24
+    }
25
+
22 26
     fly(fromX: number, fromY: number, angle: number) {
23 27
         const beforeBird = this.game.add.sprite(fromX, fromY, 'before-bird');
24 28
         beforeBird.anchor.setTo(0.5, 0.5);
... ...
@@ -1,7 +1,8 @@
1 1
 /// <reference path="../../typings/phaser.d.ts"/>
2 2
 import { ShmuprpgGame } from "../ShmuprpgGame";
3
+import {Vulnerable} from "./features/Vulnerable.ts";
3 4
 
4
-export class Bullet extends Phaser.Sprite {
5
+export class Bullet extends Phaser.Sprite implements Vulnerable {
5 6
 
6 7
     constructor(game: Phaser.Game) {
7 8
         super(game, 0, 0, 'bullets');
... ...
@@ -12,7 +13,11 @@ export class Bullet extends Phaser.Sprite {
12 12
         this.outOfBoundsKill = true;
13 13
         this.exists = false;
14 14
     }
15
-    
15
+
16
+    getVulnerableRectangles(): Array<Phaser.Rectangle> {
17
+        return [new Phaser.Rectangle(this.x, this.y, this.width, this.height)];
18
+    }
19
+
16 20
     fire(fromX: number, fromY: number, angle: number, speed: number, gravityX: number, gravityY: number) {
17 21
         this.reset(fromX, fromY, 1);
18 22
         this.scale.set(1);
... ...
@@ -2,8 +2,9 @@
2 2
 import { ShmuprpgGame } from "../ShmuprpgGame.ts";
3 3
 import { MachineGun } from "./MachineGun.ts";
4 4
 import { Controls } from "../utils/Controls.ts";
5
+import {Vulnerable} from "./features/Vulnerable.ts";
5 6
 
6
-export class Hero extends Phaser.Sprite {
7
+export class Hero extends Phaser.Sprite implements Vulnerable {
7 8
 
8 9
     weapon: MachineGun;
9 10
     controls: Controls;
... ...
@@ -26,6 +27,10 @@ export class Hero extends Phaser.Sprite {
26 26
         this.game.add.existing(this.weapon);
27 27
     }
28 28
 
29
+    getVulnerableRectangles(): Array<Phaser.Rectangle> {
30
+        return [new Phaser.Rectangle(this.x, this.y, this.width, this.height)];
31
+    }
32
+
29 33
     static preload(game: Phaser.Game) {
30 34
         game.load.atlasXML('tobira', 'sprites/lpc/characters/tobira.png', 'sprites/lpc/characters/lpc.xml');
31 35
         game.load.atlasXML('bullets', 'sprites/lpc/shootemup/effects01.png', 'sprites/lpc/shootemup/bullets.xml');
... ...
@@ -47,18 +47,22 @@ export class Pathfinder {
47 47
     findPath(startX: number, startY: number, endX: number, endY: number, callback: (path: Phaser.Point[]) => void) {
48 48
         const tileStart = this.map.getTileWorldXY(startX, startY);
49 49
         const tileEnd = this.findNearestWalkableTile(endX, endY);
50
+        if (!tileStart) {
51
+            console.log('cannot find path starting out of the map: (' + startX + ';' + startY + ')');
52
+            return;
53
+        }
50 54
         if (tileEnd) {
51 55
             this.easystar.findPath(tileStart.x, tileStart.y, tileEnd.x, tileEnd.y, (path: { x: number, y: number }[]) => {
52 56
                 let worldPath: Array<Phaser.Point>;
53
-                if (path && path.length>1) {
54
-                    worldPath = new Array<Phaser.Point>(path.length-1);
57
+                if (path && path.length > 1) {
58
+                    worldPath = new Array<Phaser.Point>(path.length - 1);
55 59
                     for (let i = 1; i < path.length; ++i) {
56 60
                         const tile = this.map.getTile(path[i].x, path[i].y);
57
-                        worldPath[i-1] = new Phaser.Point(tile.worldX + tile.width / 2, tile.worldY + tile.height / 2);
61
+                        worldPath[i - 1] = new Phaser.Point(tile.worldX + tile.width / 2, tile.worldY + tile.height / 2);
58 62
                     }
59 63
                 } else {
60 64
                     worldPath = new Array<Phaser.Point>();
61
-                }                
65
+                }
62 66
                 callback(worldPath);
63 67
             });
64 68
         } else {
... ...
@@ -94,12 +94,12 @@ export class Level extends AbstractState {
94 94
     }
95 95
 
96 96
     resolveWeaponsEffects() {
97
-        this.damageResolver.resolve(this.hero.weapon, this.birdFlock);
98
-        this.damageResolver.resolve(this.hero, this.birdFlock);
99
-        this.damageResolver.resolve(this.hero.weapon, this.grobelinHorde);
100
-        this.damageResolver.resolve(this.hero.weapon, this.spiderHorde);
97
+        this.damageResolver.groupVersusGroup(this.hero.weapon, this.birdFlock);
98
+        this.damageResolver.spriteVersusGroup(this.hero, this.birdFlock);
99
+        this.damageResolver.groupVersusGroup(this.hero.weapon, this.grobelinHorde);
100
+        this.damageResolver.groupVersusGroup(this.hero.weapon, this.spiderHorde);
101 101
         for (let spider of this.spiderHorde.children) {
102
-            this.damageResolver.resolve(this.hero, (<Spider>spider).machineGun);
102
+            this.damageResolver.spriteVersusGroup(this.hero, (<Spider>spider).machineGun);
103 103
         }
104 104
     }
105 105
 
... ...
@@ -8,26 +8,38 @@ export class DamageResolver {
8 8
         this.game = game;
9 9
     }
10 10
 
11
-    resolve(spriteOrGroupA: Phaser.Sprite | Phaser.Group, spriteOrGroupB: Phaser.Sprite | Phaser.Group) {
12
-        this.game.physics.arcade.overlap(spriteOrGroupA, spriteOrGroupB, (a: Phaser.Sprite, b: Phaser.Sprite) => {
13
-            const vulnerableA = a as (Phaser.Sprite & Vulnerable);
14
-            if (DamageResolver.checkIfSpritesIntersect(a, b)) {
15
-                a.damage(1);
16
-                b.damage(1);
11
+    groupVersusGroup(groupA: Phaser.Group, groupB: Phaser.Group) {
12
+        for (let spriteA of groupA.children) {
13
+            if (spriteA instanceof Phaser.Sprite) {
14
+                this.spriteVersusGroup(spriteA, groupB);
17 15
             }
18
-        });
16
+        }
17
+    }
18
+
19
+    spriteVersusGroup(spriteA: Phaser.Sprite, groupB: Phaser.Group) {
20
+        if (spriteA.exists) {
21
+            for (let spriteB of groupB.children) {
22
+                if (spriteB instanceof Phaser.Sprite) {
23
+                    this.spriteVersusSprite(spriteA, spriteB);
24
+                }
25
+            }
26
+        }
19 27
     }
20 28
 
29
+    spriteVersusSprite(spriteA: Phaser.Sprite, spriteB: Phaser.Sprite) {
30
+        if (spriteA.exists && spriteB.exists && DamageResolver.checkIfSpritesIntersect(spriteA, spriteB)) {
31
+            spriteA.damage(1);
32
+            spriteB.damage(1);
33
+        }
34
+    }
21 35
 
22 36
     static checkIfSpritesIntersect(a: Phaser.Sprite, b: Phaser.Sprite): boolean {
23 37
         let vulnerableRectangleOfA = DamageResolver.getVulnerableRectanglesOf(a);
24 38
         let vulnerableRectangleOfB = DamageResolver.getVulnerableRectanglesOf(b);
25
-        if (vulnerableRectangleOfA || vulnerableRectangleOfB) {
26
-            vulnerableRectangleOfA = vulnerableRectangleOfA || [new Phaser.Rectangle(a.x, a.y, a.width, a.height)];
27
-            vulnerableRectangleOfB = vulnerableRectangleOfB || [new Phaser.Rectangle(b.x, b.y, b.width, b.height)];
39
+        if (vulnerableRectangleOfA && vulnerableRectangleOfB) {
28 40
             return this.checkIfRectanglesIntersect(vulnerableRectangleOfA, vulnerableRectangleOfB);
29 41
         } else {
30
-            return true;
42
+            return false;
31 43
         }
32 44
     }
33 45