Browse Source

Got a switch to display a change when someone
touches it, will be working on a button that toggles
on when stepped on and off when stepped off. Assets temporary.

Ryan Dahlke 2 years ago
parent
commit
5e35f57ea8
4 changed files with 125 additions and 16 deletions
  1. BIN
      public/assets/button.png
  2. BIN
      public/assets/buttonA.png
  3. 124 15
      src/HelloWorldScene.ts
  4. 1 1
      vite.config.ts

BIN
public/assets/button.png


BIN
public/assets/buttonA.png


+ 124 - 15
src/HelloWorldScene.ts

@@ -1,35 +1,144 @@
 import Phaser from 'phaser'
 
 export default class HelloWorldScene extends Phaser.Scene {
+	private switches?: Phaser.Physics.Arcade.Group;
+	private switchesA?: Phaser.Physics.Arcade.Group;
+	private buttons?: Phaser.Physics.Arcade.Group;
+	private platforms?: Phaser.Physics.Arcade.StaticGroup;
+	private player?: Phaser.Physics.Arcade.Sprite;
+	private cursors?: Phaser.Types.Input.Keyboard.CursorKeys;
+	
 	constructor() {
 		super('hello-world')
 	}
 
 	preload() {
-		this.load.setBaseURL('https://labs.phaser.io')
+		//this.load.setBaseURL('https://labs.phaser.io')
 
-		this.load.image('sky', 'assets/skies/space3.png')
-		this.load.image('logo', 'assets/sprites/phaser3-logo.png')
-		this.load.image('red', 'assets/particles/red.png')
+		this.load.image('sky', 'assets/sky.png')
+		this.load.image("switch", "assets/star.png");
+		this.load.image("switchA", "assets/bomb.png");
+		this.load.image("button", "assets/button.png");
+		this.load.image("buttonA", "assets/buttonA.png");
+		this.load.image("ground", "assets/platform.png");
+		this.load.spritesheet("dude", "assets/dude.png",{
+			frameWidth: 32, frameHeight: 48
+		});
+		
 	}
 
 	create() {
-		this.add.image(400, 300, 'sky')
+		//Makes sky box
+		this.add.image(400, 300, 'sky');
+		// Code Related to platforms
+		this.platforms = this.physics.add.staticGroup()
+		const ground = this.platforms.create(400,568, "ground") as Phaser.Physics.Arcade.Sprite
+		ground.setScale(2)
+		ground.refreshBody()
+		//Add Platform(s)
 
-		const particles = this.add.particles('red')
+		//this.platforms.create(600,400,"ground")
 
-		const emitter = particles.createEmitter({
-			speed: 100,
-			scale: { start: 1, end: 0 },
-			blendMode: 'ADD',
+		//Code related to the player
+		this.player = this.physics.add.sprite(100,430, "dude")
+		this.player.setBounce(0.2)
+		this.player.setCollideWorldBounds(true)
+		this.physics.add.collider(this.player, this.platforms)
+
+		this.anims.create({
+			key: "left",
+			frames: this.anims.generateFrameNumbers("dude", {
+				start: 0, end: 3
+			}),
+			frameRate: 10,
+			repeat: -1
+		})
+
+		this.anims.create({
+			key: "turn",
+			frames: [{key: "dude", frame: 4 }],
+			frameRate: 20
 		})
 
-		const logo = this.physics.add.image(400, 100, 'logo')
+		this.anims.create({
+			key: "right",
+			frames: this.anims.generateFrameNumbers("dude", {
+				start: 5, end: 8
+			}),
+			frameRate: 10,
+			repeat: -1
+		})
+
+		this.cursors = this.input.keyboard.createCursorKeys()
 
-		logo.setVelocity(100, 200)
-		logo.setBounce(1, 1)
-		logo.setCollideWorldBounds(true)
+		//Code related to switches
+		this.switches = this.physics.add.group({
+			key: "switch",
+			setXY: {x:240, y:450}
+		})
+		this.physics.add.collider(this.switches, this.platforms)
+		this.physics.add.overlap(this.player, this.switches, this.handleHitSwitch, undefined, this)
 
-		emitter.startFollow(logo)
+		this.switchesA = this.physics.add.group({
+		key: "switchA",
+			setXY: {x:240, y:450}
+		})
+		this.physics.add.collider(this.switchesA, this.platforms)
+
+		this.physics.add.overlap(this.switchesA, this.switches, this.handleSwitchSetup, undefined, this)
+		this.physics.add.overlap(this.switchesA, this.player, this.handleHitSwitchA, undefined, this)
+
+		this.buttons = this.physics.add.group({
+			key: "button",
+			setXY: {x:440, y:450}
+		})
+		this.physics.add.collider(this.buttons, this.platforms)
+		this.physics.add.overlap(this.player, this.buttons, this.handleHitButon, undefined, this)
+		
+		
 	}
+//Handle buttons
+	private handleHitButon(player: Phaser.GameObjects.GameObject, b:Phaser.GameObjects.GameObject){
+			
+		}
+//Handle switches
+private handleSwitchSetup(sA: Phaser.GameObjects.GameObject, s:Phaser.GameObjects.GameObject){
+	const the_switch = sA as Phaser.Physics.Arcade.Image
+	the_switch.visible = false
+}
+
+private handleHitSwitch(player: Phaser.GameObjects.GameObject, s:Phaser.GameObjects.GameObject){
+	const the_switch = s as Phaser.Physics.Arcade.Image
+	the_switch.disableBody(true,true)
+}
+private handleHitSwitchA(player: Phaser.GameObjects.GameObject, sA:Phaser.GameObjects.GameObject){
+	const the_switch = sA as Phaser.Physics.Arcade.Image
+	the_switch.visible = true
+}
+
+
+	update(){
+		if(!this.cursors){
+			return
+		}
+
+		if(this.cursors?.left.isDown){
+			this.player?.setVelocityX(-160)
+			this.player?.anims.play("left", true)
+		}
+		else if(this.cursors?.right.isDown){
+			this.player?.setVelocityX(160)
+			this.player?.anims.play("right", true)
+		}
+		else{
+			this.player?.setVelocityX(0)
+			this.player?.anims.play("turn")
+		}
+
+		if(this.cursors.up?.isDown && this.player?.body.touching.down){
+			this.player.setVelocityY(-330)
+		}
+
+	}
+
 }

+ 1 - 1
vite.config.ts

@@ -2,6 +2,6 @@ import { defineConfig } from 'vite'
 
 export default defineConfig({
 	plugins: [],
-	server: { host: '0.0.0.0', port: 8000 },
+	server: { host: '0.0.0.0', port: 2332 },
 	clearScreen: false,
 })