Browse code

simplify collisions

devnewton authored on 14/10/2016 at 16:50:15
Showing 8 changed files
... ...
@@ -1,7 +1,6 @@
1 1
 /// <reference path="../../typings/phaser.d.ts"/>
2
-import {Vulnerable} from "./features/Vulnerable.ts";
3 2
 
4
-export class Bird extends Phaser.Sprite implements Vulnerable {
3
+export class Bird extends Phaser.Sprite {
5 4
 
6 5
     birdExplosion: Phaser.Sprite;
7 6
 
... ...
@@ -20,10 +19,6 @@ export class Bird extends Phaser.Sprite implements Vulnerable {
20 20
         explodeAnimation.killOnComplete = true;
21 21
     }
22 22
 
23
-    getVulnerableRectangles(): Array<Phaser.Rectangle> {
24
-        return [new Phaser.Rectangle(this.x, this.y, this.width, this.height)];
25
-    }
26
-
27 23
     fly(fromX: number, fromY: number, angle: number) {
28 24
         const beforeBird = this.game.add.sprite(fromX, fromY, 'before-bird');
29 25
         beforeBird.anchor.setTo(0.5, 0.5);
... ...
@@ -44,4 +39,5 @@ export class Bird extends Phaser.Sprite implements Vulnerable {
44 44
         this.birdExplosion.play('explode', 8, false);
45 45
         return this;
46 46
     }
47
+    
47 48
 }
... ...
@@ -1,8 +1,7 @@
1 1
 /// <reference path="../../typings/phaser.d.ts"/>
2 2
 import { ShmuprpgGame } from "../ShmuprpgGame";
3
-import {Vulnerable} from "./features/Vulnerable.ts";
4 3
 
5
-export class Bullet extends Phaser.Sprite implements Vulnerable {
4
+export class Bullet extends Phaser.Sprite {
6 5
 
7 6
     bulletAnimation: string;
8 7
 
... ...
@@ -17,11 +16,6 @@ export class Bullet extends Phaser.Sprite implements Vulnerable {
17 17
         this.exists = false;
18 18
     }
19 19
 
20
-    getVulnerableRectangles(): Array<Phaser.Rectangle> {
21
-        let b = this.getBounds();
22
-        return [new Phaser.Rectangle(b.x, b.y, b.width, b.height)];
23
-    }
24
-
25 20
     fire(fromX: number, fromY: number, angle: number, speed: number, gravityX: number, gravityY: number) {
26 21
         this.reset(fromX, fromY, 1);
27 22
         this.scale.set(1);
... ...
@@ -2,9 +2,8 @@
2 2
 import {Level} from "../states/Level.ts";
3 3
 import {Pathfinder} from "../ia/services/Pathfinder.ts";
4 4
 import * as b3 from "../ia/decisions/b3.ts";
5
-import {Vulnerable} from "./features/Vulnerable.ts";
6 5
 
7
-export class Grobelin extends Phaser.Sprite implements Vulnerable {
6
+export class Grobelin extends Phaser.Sprite {
8 7
 
9 8
     grobelinDeath: Phaser.Sprite;
10 9
     enemy: Phaser.Sprite;
... ...
@@ -27,11 +26,6 @@ export class Grobelin extends Phaser.Sprite implements Vulnerable {
27 27
         this.grobelinDeath.exists = false;
28 28
     }
29 29
 
30
-    getVulnerableRectangles(): Array<Phaser.Rectangle> {
31
-        let b = this.getBounds();
32
-        return [new Phaser.Rectangle(b.x, b.y, b.width, b.height)];
33
-    }
34
-
35 30
     appears(fromX: number, fromY: number, target: Phaser.Sprite) {
36 31
         const beforeGrobelin = this.game.add.sprite(fromX, fromY, 'before-bird');
37 32
         beforeGrobelin.anchor.setTo(0.5, 0.5);
... ...
@@ -2,10 +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";
6 5
 import {Bullet} from "./Bullet.ts";
7 6
 
8
-export class Hero extends Phaser.Sprite implements Vulnerable {
7
+export class Hero extends Phaser.Sprite {
9 8
 
10 9
     weapon: HeroMachineGun;
11 10
     controls: Controls;
... ...
@@ -28,11 +27,6 @@ export class Hero extends Phaser.Sprite implements Vulnerable {
28 28
         this.game.add.existing(this.weapon);
29 29
     }
30 30
 
31
-    getVulnerableRectangles(): Array<Phaser.Rectangle> {
32
-        let b = this.getBounds();
33
-        return [new Phaser.Rectangle(b.x, b.y, b.width, b.height)];
34
-    }
35
-
36 31
     static preload(game: Phaser.Game) {
37 32
         game.load.atlasXML('tobira', 'sprites/lpc/characters/tobira.png', 'sprites/lpc/characters/lpc.xml');
38 33
         game.load.atlasXML('bullets', 'sprites/lpc/shootemup/effects01.png', 'sprites/lpc/shootemup/bullets.xml');
... ...
@@ -2,10 +2,9 @@
2 2
 import {Level} from "../states/Level.ts";
3 3
 import {Pathfinder} from "../ia/services/Pathfinder.ts";
4 4
 import * as b3 from "../ia/decisions/b3.ts";
5
-import {Vulnerable} from "./features/Vulnerable.ts";
6 5
 import {MachineGun} from "./MachineGun.ts";
7 6
 
8
-export class Spider extends Phaser.Sprite implements Vulnerable {
7
+export class Spider extends Phaser.Sprite {
9 8
 
10 9
     spiderDeath: Phaser.Sprite;
11 10
     enemy: Phaser.Sprite;
... ...
@@ -30,11 +29,6 @@ export class Spider extends Phaser.Sprite implements Vulnerable {
30 30
         this.machineGun = new MachineGun(this.game, 1);
31 31
     }
32 32
 
33
-    getVulnerableRectangles(): Array<Phaser.Rectangle> {
34
-        let b = this.getBounds();
35
-        return [new Phaser.Rectangle(b.x, b.y, b.width, b.height)];
36
-    }
37
-
38 33
     appears(fromX: number, fromY: number, target: Phaser.Sprite) {
39 34
         const beforeSpider = this.game.add.sprite(fromX, fromY, 'before-bird');
40 35
         beforeSpider.anchor.setTo(0.5, 0.5);
41 36
deleted file mode 100644
... ...
@@ -1,5 +0,0 @@
1
-/// <reference path="../../../typings/phaser.d.ts"/>
2
-
3
-export interface Vulnerable {
4
-    getVulnerableRectangles(): Array<Phaser.Rectangle>;
5
-}
6 1
\ No newline at end of file
... ...
@@ -6,9 +6,6 @@ import {GrobelinHorde} from "../entities/GrobelinHorde.ts";
6 6
 import {SpiderHorde} from "../entities/SpiderHorde.ts";
7 7
 import {Spider} from "../entities/Spider.ts";
8 8
 
9
-import {Vulnerable} from "../entities/features/Vulnerable.ts";
10
-
11
-
12 9
 import {Pathfinder} from "../ia/services/Pathfinder.ts";
13 10
 import {DamageResolver} from "../utils/DamageResolver.ts";
14 11
 
... ...
@@ -84,12 +81,12 @@ export class Level extends AbstractState {
84 84
         this.spiderHorde = new SpiderHorde(this.hero, this.pathfinder, 0);
85 85
         this.game.add.existing(this.spiderHorde);
86 86
 
87
- this.game.time.events.add(1000, () => this.birdFlock.reset(this.hero, 10));
88
-        /*
87
+        //this.game.time.events.add(1000, () => this.birdFlock.reset(this.hero, 10));
88
+        
89 89
         this.game.time.events.add(1000, () => this.grobelinHorde.reset(this.hero, 3));
90 90
         this.game.time.events.add(60000, () => this.spiderHorde.reset(this.hero, 4));
91 91
         this.game.time.events.add(12000, () => this.birdFlock.reset(this.hero, 10));
92
-        */
92
+        
93 93
     }
94 94
 
95 95
     update() {
... ...
@@ -110,11 +107,5 @@ export class Level extends AbstractState {
110 110
     }
111 111
 
112 112
     render() {
113
-        for(let b of this.birdFlock.children) {
114
-            let vulnerable = <Vulnerable><any>b;
115
-            for(let v of vulnerable.getVulnerableRectangles()) {
116
-                this.game.debug.rectangle(v);
117
-            }
118
-         }
119 113
     }
120 114
 }
... ...
@@ -1,6 +1,4 @@
1 1
 /// <reference path="../../typings/phaser.d.ts"/>
2
-import {Vulnerable} from "../entities/features/Vulnerable.ts";
3
-
4 2
 export class DamageResolver {
5 3
     game: Phaser.Game;
6 4
 
... ...
@@ -27,42 +25,10 @@ export class DamageResolver {
27 27
     }
28 28
 
29 29
     spriteVersusSprite(spriteA: Phaser.Sprite, spriteB: Phaser.Sprite) {
30
-        if (spriteA.exists && spriteB.exists && DamageResolver.checkIfSpritesIntersect(spriteA, spriteB)) {
30
+        if (spriteA.exists && spriteB.exists && spriteA.overlap(spriteB)) {
31 31
             spriteA.damage(1);
32 32
             spriteB.damage(1);
33 33
         }
34 34
     }
35 35
 
36
-    static checkIfSpritesIntersect(a: Phaser.Sprite, b: Phaser.Sprite): boolean {
37
-        let vulnerableRectangleOfA = DamageResolver.getVulnerableRectanglesOf(a);
38
-        let vulnerableRectangleOfB = DamageResolver.getVulnerableRectanglesOf(b);
39
-        if (vulnerableRectangleOfA && vulnerableRectangleOfB) {
40
-            return this.checkIfRectanglesIntersect(vulnerableRectangleOfA, vulnerableRectangleOfB);
41
-        } else {
42
-            return false;
43
-        }
44
-    }
45
-
46
-    static checkIfRectanglesIntersect(rectanglesA: Array<Phaser.Rectangle>, rectanglesB: Array<Phaser.Rectangle>): boolean {
47
-        for (let a of rectanglesA) {
48
-            for (let b of rectanglesB) {
49
-                if (a.intersects(b, 0)) {
50
-                    return true;
51
-                }
52
-            }
53
-        }
54
-        return false;
55
-    }
56
-
57
-    static getVulnerableRectanglesOf(s: Phaser.Sprite | (Phaser.Sprite & Vulnerable)) {
58
-        let result: Array<Phaser.Rectangle>;
59
-        if (DamageResolver.isVulnerable(s)) {
60
-            result = (s as Vulnerable).getVulnerableRectangles();
61
-        }
62
-        return result;
63
-    }
64
-
65
-    static isVulnerable(s: any): s is Vulnerable {
66
-        return s.getVulnerableRectangles != undefined;
67
-    }
68 36
 }
69 37
\ No newline at end of file