Parcourir la source

✨ Start work on the player controller for mobile devices on FoodHunt

Pedro Tonini Rosenberg Schneider il y a 3 ans
Parent
commit
89e81d4816

+ 1 - 0
index.html

@@ -22,6 +22,7 @@
     <!-- Earth -->
     <script type="text/javascript" src="src/elements/earth/EarthMinigameSelector.js"></script>
     <!-- Food Hunt -->
+    <script type="text/javascript" src="src/elements/earth/foodHunt/FoodHuntMobileController.js"></script>
     <script type="text/javascript" src="src/elements/earth/foodHunt/FoodHuntDialogue.js"></script>
     <script type="text/javascript" src="src/elements/earth/foodHunt/FoodHuntFruit.js"></script>
     <script type="text/javascript" src="src/elements/earth/foodHunt/FoodHuntBasket.js"></script>

+ 13 - 4
src/elements/earth/foodHunt/FoodHuntGame.js

@@ -27,7 +27,7 @@ class FoodHuntGame extends Object2D
 
         /** @type {Button} */
         this.backButton = null;
-    
+
         /** @type {FoodHuntPlayer} */
         this.player = null;
         /** @type {FoodHuntTree} */
@@ -38,7 +38,9 @@ class FoodHuntGame extends Object2D
         this.basket = null;
         /** @type {FoodHuntDialogue} */
         this.dialogue = null;
-    
+        /** @type {FoodHuntMobileController} */
+        this.mobileController = null;
+
         /** @type {Timer} */
         this.initialTimer = null;
         /** @type {Timer} */
@@ -47,12 +49,12 @@ class FoodHuntGame extends Object2D
         this.fruitsTimer = null;
         /** @type {Timer} */
         this.endGameTimer = null;
-    
+
         /** @type {Boolean} */
         this.gameStarted = false;
         /** @type {Boolean} */
         this.gameEnded = false;
-    
+
         /** @type {Number} */
         this.points = 0;
     }
@@ -104,6 +106,13 @@ class FoodHuntGame extends Object2D
         this.endGameTimer = new Timer("EndGametimer", 1, false, true);
         this.endGameTimer.connect("timeout", this, "_onEndGameTimerTimeout");
         this.addChild(this.endGameTimer);
+
+        this.mobileController = new FoodHuntMobileController("MobileController");
+        this.mobileController.connect("moveLeft", this.player, "_onMobileControllerMoveLeft");
+        this.mobileController.connect("stopLeft", this.player, "_onMobileControllerStopLeft");
+        this.mobileController.connect("moveRight", this.player, "_onMobileControllerMoveRight");
+        this.mobileController.connect("stopRight", this.player, "_onMobileControllerStopRight");
+        this.addChild(this.mobileController);
     }
 
     _draw( /** @type {number} */ delta, /** @type {p5.Graphics} */ db)

+ 50 - 0
src/elements/earth/foodHunt/FoodHuntMobileController.js

@@ -0,0 +1,50 @@
+class FoodHuntMobileController extends Object2D
+{
+    constructor(name)
+    {
+        super(name);
+    }
+
+    _initSignals()
+    {
+        this.addSignal("moveLeft");
+        this.addSignal("stopLeft");
+        this.addSignal("moveRight");
+        this.addSignal("stopRight");
+    }
+
+    _setup()
+    {
+        var area = new Area2D("LeftArea", SHAPES.RECT, new Rect(100, 100), true, true);
+        area.setPosition(70, 1080 - 70);
+        area.connect("mouseEntered", this, "_onLeftAreaMouseEntered");
+        area.connect("mouseExited", this, "_onLeftAreaMouseExited");
+        this.addChild(area);
+
+        var area2 = new Area2D("RightArea", SHAPES.RECT, new Rect(100, 100), true, true);
+        area2.setPosition(1920 - 70, 1080 - 70);
+        area2.connect("mouseEntered", this, "_onRightAreaMouseEntered");
+        area2.connect("mouseExited", this, "_onRightAreaMouseExited");
+        this.addChild(area2);
+    }
+
+    _onLeftAreaMouseEntered()
+    {
+        this.emitSignal("moveLeft")
+    }
+
+    _onLeftAreaMouseExited()
+    {
+        this.emitSignal("stopLeft");
+    }
+
+    _onRightAreaMouseEntered()
+    {
+        this.emitSignal("moveRight");
+    }
+
+    _onRightAreaMouseExited()
+    {
+        this.emitSignal("stopRight");
+    }
+}

+ 27 - 2
src/elements/earth/foodHunt/FoodHuntPlayer.js

@@ -27,6 +27,11 @@ class FoodHuntPlayer extends Object2D
 
         /** @type {Number} */
         this.direction = 0;
+
+        /** @type {Boolean} */
+        this.moveLeft = false;
+        /** @type {Boolean} */
+        this.moveRight = false;
     }
 
     _setup()
@@ -37,8 +42,8 @@ class FoodHuntPlayer extends Object2D
     _update( /** @type {Number} */ delta)
     {
         this.direction = 0;
-        if (keyIsDown(LEFT_ARROW)) this.direction -= 1;
-        if (keyIsDown(RIGHT_ARROW)) this.direction += 1;
+        if (keyIsDown(LEFT_ARROW) || this.moveLeft) this.direction -= 1;
+        if (keyIsDown(RIGHT_ARROW) || this.moveRight) this.direction += 1;
     }
 
     _draw( /** @type {Number} */ delta, /** @type {p5.Graphics} */ db)
@@ -50,4 +55,24 @@ class FoodHuntPlayer extends Object2D
         if (this.position.x >= 1920 - 50) this.position.x = 1920 - 55;
         else if (this.position.x <= 55) this.position.x = 55;
     }
+
+    _onMobileControllerMoveLeft()
+    {
+        this.moveLeft = true;
+    }
+
+    _onMobileControllerStopLeft()
+    {
+        this.moveLeft = false;
+    }
+
+    _onMobileControllerMoveRight()
+    {
+        this.moveRight = true;
+    }
+
+    _onMobileControllerStopRight()
+    {
+        this.moveRight = false;
+    }
 }