Skip to content

Commit

Permalink
Initial submarine controls
Browse files Browse the repository at this point in the history
Co-authored-by: ceddy6 <ceddy6@users.noreply.github.com>
Co-authored-by: wgilmour@hotmail.co.uk <wgilmour@hotmail.co.uk>
  • Loading branch information
3 people committed Apr 24, 2021
1 parent a4c293b commit 2bbd6ba
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 19 deletions.
Binary file added src/assets/img/submarine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/scripts/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const config = {
default: 'arcade',
arcade: {
debug: false,
gravity: { y: 400 }
gravity: { y: 0 }
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/scripts/objects/Submarine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export default class Submarine extends Phaser.Physics.Arcade.Sprite {
// Constructor for submarine
constructor(scene, x, y) {
// Create submarine
super(scene, x, y, 'submarine')
scene.add.existing(this)
scene.physics.add.existing(this)

// Set physics params
this.body.setMass(100)
this.setDamping(true)
this.setDrag(0.25)
this.setMaxVelocity(200)
this.setCollideWorldBounds(false)
}

// Update loop - game physics tbc
update(cursors: Phaser.Types.Input.Keyboard.CursorKeys) {
// X direction - assume no key pressed
let somethingDownX = false
// Check for left and right keys
if (cursors.left.isDown) {
somethingDownX = true
this.setAccelerationX(-100)
this.setFlip(true, false)
} else if (cursors.right.isDown) {
somethingDownX = true
this.setAccelerationX(100)
this.setFlip(false, false)
}
// If still no key pressed, set horizontal acceleration to 0
if (!somethingDownX) this.setAccelerationX(0)

// Y direction - assume no key pressed
let somethingDownY = false
// Check for up and down keys
if (cursors.up.isDown) {
somethingDownY = true
this.setAccelerationY(-100)
} else if (cursors.down.isDown) {
somethingDownY = true
this.setAccelerationY(100)
}
// If still no key pressed, set vertical acceleration to 0
if (!somethingDownY) this.setAccelerationY(0)
}
}
14 changes: 0 additions & 14 deletions src/scripts/objects/phaserLogo.ts

This file was deleted.

10 changes: 7 additions & 3 deletions src/scripts/scenes/mainScene.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import PhaserLogo from '../objects/phaserLogo'
import Submarine from '../objects/Submarine'
import FpsText from '../objects/fpsText'

export default class MainScene extends Phaser.Scene {
fpsText
fpsText: FpsText
cursor: Phaser.Types.Input.Keyboard.CursorKeys
submarine: Submarine

constructor() {
super({ key: 'MainScene' })
}

create() {
new PhaserLogo(this, this.cameras.main.width / 2, 0)
this.submarine = new Submarine(this, this.cameras.main.width / 2, 0)
this.fpsText = new FpsText(this)

// display the Phaser.VERSION
Expand All @@ -19,9 +21,11 @@ export default class MainScene extends Phaser.Scene {
fontSize: '24px'
})
.setOrigin(1, 0)
this.cursor = this.input.keyboard.createCursorKeys()
}

update() {
this.submarine.update(this.cursor)
this.fpsText.update()
}
}
2 changes: 1 addition & 1 deletion src/scripts/scenes/preloadScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default class PreloadScene extends Phaser.Scene {
}

preload() {
this.load.image('phaser-logo', 'assets/img/phaser-logo.png')
this.load.image('submarine', 'assets/img/submarine.png')
}

create() {
Expand Down

0 comments on commit 2bbd6ba

Please sign in to comment.