Browse Source

Merge pull request #12 from Spring-2023-CISC374/Gates

Gates
RyanADahlke 2 years ago
parent
commit
28049d8ec1
4 changed files with 154 additions and 9 deletions
  1. 96 9
      src/HelloWorldScene.ts
  2. 12 0
      src/Objects/Button.ts
  3. 34 0
      src/Objects/Gate.ts
  4. 12 0
      src/Objects/Switch.ts

+ 96 - 9
src/HelloWorldScene.ts

@@ -1,11 +1,19 @@
 import Phaser from 'phaser'
+import Gate from "./Objects/Gate";
+import Switch from "./Objects/Switch";
+import Button from './Objects/Button';
 
 export default class HelloWorldScene extends Phaser.Scene {
     //Sprite creation
     private switches?: Phaser.Physics.Arcade.Group;
+    private switchArray: Switch[] = [];
     private switchesA?: Phaser.Physics.Arcade.Group;
     private buttons?: Phaser.Physics.Arcade.Group;
+    private buttonArray: Button[] = [];
     private buttonsA?: Phaser.Physics.Arcade.Group;
+    private gates?: Phaser.Physics.Arcade.Group;
+    private gateArray: Gate[] = [];
+  //  private gatesA?: Phaser.Physics.Arcade.Group;
     private platforms?: Phaser.Physics.Arcade.StaticGroup;
     private boxes?: Phaser.Physics.Arcade.StaticGroup;
     private player1?: Phaser.Physics.Arcade.Sprite;
@@ -26,7 +34,9 @@ export default class HelloWorldScene extends Phaser.Scene {
         this.load.image("switchA", "assets/bomb.png");
         this.load.image("button", "assets/button.png");
         this.load.image("buttonA", "assets/buttonA.png");
+        this.load.image("gate", "assets/BowlingBall.png");
         this.load.image("ground", "assets/platform.png");
+        this.load.image("gateA", "assets/star.png");
         this.load.image("box", "assets/box.png");
         this.load.spritesheet("dude", "assets/dude.png", {
             frameWidth: 32, frameHeight: 48
@@ -129,10 +139,20 @@ export default class HelloWorldScene extends Phaser.Scene {
         this.cursors = this.input.keyboard.createCursorKeys()
 
         //Code related to switches
+        this.switchArray = []
+       this.switchArray.push(new Switch(this, 400, 500, "switch", 0, 0))
         this.switches = this.physics.add.group({
             key: "switch",
-            setXY: { x: 700, y: 60 }
+            setXY: { x: -480, y: 250 }
+ //         setXY: { x: 700, y: 60 }
+        })
+
+        
+        this.switchArray.forEach(object => {
+            this.switches?.add(object);
         })
+        
+      //  this.switches.add(switch0)
         this.physics.add.collider(this.switches, this.platforms)
         this.physics.add.overlap(this.player1, this.switches, this.handleHitSwitch1, undefined, this)
         this.physics.add.overlap(this.player2, this.switches, this.handleHitSwitch2, undefined, this)
@@ -147,11 +167,43 @@ export default class HelloWorldScene extends Phaser.Scene {
         this.physics.add.overlap(this.switchesA, this.player1, this.handleHitSwitchA1, undefined, this)
         this.physics.add.overlap(this.switchesA, this.player2, this.handleHitSwitchA2, undefined, this)
         
+        
+
         // Here you can make new switches without having to call anything else - BN
         //this.switches.create(400, 510,"switch")
         //this.switchesA.create(400, 510,"switchA")
         //
 
+        //Code Related to Gates
+        
+        this.gates = this.physics.add.group({
+            key: "gate",
+            immovable: true,
+            allowGravity: false,
+            setXY: { x: -480, y: 250 }
+        })
+/*
+        this.gatesA = this.physics.add.group({
+            key: "gateA",
+            immovable: true,
+            allowGravity: false,
+            setXY: { x: 200, y: 200 },
+        })
+        this.gatesA.create(400, 200,"gateA")
+        this.physics.add.overlap(this.gatesA, this.gates, this.handleGateSetup, undefined, this)
+*/
+       // this.gates.create(200, 0,"gate")
+        this.physics.add.collider(this.gates, this.platforms)
+        this.physics.add.collider(this.gates, this.player1)
+        this.physics.add.collider(this.gates, this.player2)
+
+        this.gateArray[0] = new Gate(this, 0, 250, "gate", 0);
+        this.gateArray[1] = new Gate(this, 200, 200, "gate", 1);
+        this.gateArray[2] = new Gate(this, 400, 200, "gate", 2);
+        
+        this.gateArray.forEach(object => {
+            this.gates?.add(object);
+        })
         // for scene transition
         if (this.nextScene) {
             this.tweens.add({
@@ -168,14 +220,22 @@ export default class HelloWorldScene extends Phaser.Scene {
         this.physics.add.overlap(this.nextScene, this.player2, this.handleLoadNextScene, undefined, this)
 
         //Code related to buttons
+        this.buttonArray = []
+       this.buttonArray.push(new Button(this, 480, 250, "button", 1, 0))
+       this.buttonArray.push(new Button(this, 300, 500, "button", 2, 0))
         this.buttons = this.physics.add.group({
             key: "button",
-            setXY: { x: 480, y: 250 }
+            setXY: { x: -480, y: 250 }
         })
+
+        this.buttonArray.forEach(object => {
+            this.buttons?.add(object);
+        })
+
         this.physics.add.collider(this.buttons, this.platforms)
         this.physics.add.overlap(this.player1, this.buttons, this.handleHitButton, undefined, this)
         this.physics.add.overlap(this.player2, this.buttons, this.handleHitButton, undefined, this)
-
+        
         this.buttonsA = this.physics.add.group({
             key: "buttonA",
             setXY: { x: 480, y: 250 }
@@ -188,7 +248,7 @@ export default class HelloWorldScene extends Phaser.Scene {
 
 
         // Here you can make new buttons without having to call anything else - BN
-        this.buttons.create(300, 500,"button")
+        
         this.buttonsA.create(300, 500,"buttonA")
         //
 
@@ -196,9 +256,17 @@ export default class HelloWorldScene extends Phaser.Scene {
 
     //Handle buttons
     private handleHitButton(player1: Phaser.GameObjects.GameObject, b: Phaser.GameObjects.GameObject) {
-
+        const the_button = b as Button
+        this.gateArray[the_button.gateID].actives[the_button.buttonID] = true;
+        this.gateArray[the_button.gateID].handleActivate;
+        //temp code
+        this.gateArray[the_button.gateID].disableBody(true,true)
+       // this.time.delayedCall(1000,  this.handleGateDeactive, undefined, this.gateArray[the_button.gateID])
+        
+        
     }
 
+
     private handleButtonSetup(bA: Phaser.GameObjects.GameObject, b: Phaser.GameObjects.GameObject) {
         const the_button = bA as Phaser.Physics.Arcade.Image
         the_button.visible = false
@@ -220,14 +288,22 @@ export default class HelloWorldScene extends Phaser.Scene {
         the_switch.visible = false
     }
 
-    private handleHitSwitch1(player1: Phaser.GameObjects.GameObject, s: Phaser.GameObjects.GameObject) {
-        const the_switch = s as Phaser.Physics.Arcade.Image
+    private handleHitSwitch1(player1: Phaser.GameObjects.GameObject, s: Switch) {
+        const the_switch = s as Switch
         the_switch.disableBody(true, true)
+        this.gateArray[the_switch.gateID].actives[the_switch.switchID] = true;
+        this.gateArray[the_switch.gateID].handleActivate;
+        //temp code
+        this.gateArray[the_switch.gateID].disableBody(true,true)
     }
 
-    private handleHitSwitch2(player2: Phaser.GameObjects.GameObject, s: Phaser.GameObjects.GameObject) {
-        const the_switch = s as Phaser.Physics.Arcade.Image
+    private handleHitSwitch2(player2: Phaser.GameObjects.GameObject, s: Switch) {
+        const the_switch = s as Switch
         the_switch.disableBody(true, true)
+        this.gateArray[the_switch.gateID].actives[the_switch.switchID] = true;
+        this.gateArray[the_switch.gateID].handleActivate;
+        //temp code
+        this.gateArray[the_switch.gateID].disableBody(true,true)
     }
 
     private handleHitSwitchA1(player1: Phaser.GameObjects.GameObject, sA: Phaser.GameObjects.GameObject) {
@@ -240,6 +316,17 @@ export default class HelloWorldScene extends Phaser.Scene {
         the_switch.visible = true
     }
 
+    //Handle Gates
+    private handleGateSetup(gA: Phaser.GameObjects.GameObject, g: Gate) {
+        const the_gate = g as Phaser.Physics.Arcade.Image
+        the_gate.enableBody(false, the_gate.x, the_gate.y, true, true)
+    }
+
+    private handleGateDeactive(g:Gate){
+        const the_gate = g as Phaser.Physics.Arcade.Image
+        the_gate.enableBody(false, the_gate.x, the_gate.y, true, true)
+    }
+
 	// sence transition
     private handleLoadNextScene(player: Phaser.GameObjects.GameObject, sA: Phaser.GameObjects.GameObject) {
         this.scene.start('TwoScene')

+ 12 - 0
src/Objects/Button.ts

@@ -0,0 +1,12 @@
+import Phaser from "phaser";
+
+export default class Button extends Phaser.Physics.Arcade.Image{
+    gateID: number;
+    buttonID: number;
+    constructor(scene: Phaser.Scene, x: number, y: number, texture: string, gateID:number, buttonID:number) {
+        super(scene, x, y, texture);
+        this.gateID = gateID;
+        this.buttonID = buttonID;
+        scene.add.existing(this);
+    }
+}

+ 34 - 0
src/Objects/Gate.ts

@@ -0,0 +1,34 @@
+import Phaser from "phaser";
+
+export default class Gate extends Phaser.Physics.Arcade.Image{
+    actives: boolean[] = [true, true, true]
+
+    constructor(scene: Phaser.Scene, x: number, y: number, texture: string, actives:number) {
+        super(scene, x, y, texture);
+        /*
+        for(let i = 0; i < actives && i < 3; i++){
+            this.actives[i] = false
+        }
+        */
+        scene.add.existing(this);
+    }
+
+    handleActivate(){
+        if(this.actives[0] && this.actives[1] && this.actives[2]){
+            this.setVisible(false)
+        }
+        return;
+    }
+
+    handleDeactivate(){
+        if(this.actives[0] && this.actives[1] && this.actives[2]){
+            return;
+        }
+        else{
+            this.setVisible(true)
+        }
+        return;
+    }
+
+
+}

+ 12 - 0
src/Objects/Switch.ts

@@ -0,0 +1,12 @@
+import Phaser from "phaser";
+
+export default class Switch extends Phaser.Physics.Arcade.Image{
+    gateID: number;
+    switchID: number;
+    constructor(scene: Phaser.Scene, x: number, y: number, texture: string, gateID:number, switchID:number) {
+        super(scene, x, y, texture);
+        this.gateID = gateID;
+        this.switchID = switchID;
+        scene.add.existing(this);
+    }
+}