Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
69 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { GameObjects } from 'phaser'; | ||
|
||
/** | ||
* ShooterImage is the base class for all images in our space shooter game. | ||
* | ||
* Technically, a shooter image is a Phaser Image. Learn more about images at | ||
* https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Image.html. | ||
**/ | ||
export class ShooterImage extends GameObjects.Image { | ||
protected speed: number; | ||
protected sceneHeight: number; | ||
protected sceneWidth: number; | ||
|
||
constructor(scene: Phaser.Scene, texture: string, speedDistance: number) { | ||
// Initialize the Phaser image | ||
super(scene, 0, 0, texture); | ||
|
||
// Store the size of the scene, we will need it later | ||
this.sceneHeight = scene.game.canvas.height; | ||
this.sceneWidth = scene.game.canvas.width; | ||
|
||
// Calculate the speed | ||
this.speed = Phaser.Math.GetSpeed(speedDistance, 1); | ||
} | ||
|
||
protected activate() { | ||
this.setActive(true); | ||
this.setVisible(true); | ||
} | ||
|
||
kill() { | ||
this.setActive(false); | ||
this.setVisible(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ShooterImage } from './shooterImage'; | ||
|
||
/** Possible movement directions */ | ||
export enum Direction { | ||
Left = -1, | ||
Right = 1 | ||
} | ||
|
||
/** Spaceship that can move left and right */ | ||
export class Spaceship extends ShooterImage { | ||
constructor(scene: Phaser.Scene) { | ||
super(scene, 'ship', 200); | ||
|
||
// Position the spaceship horizontally in the middle of the screen | ||
// and vertically at the bottom of the screen. | ||
this.setPosition(scene.game.canvas.width / 2, scene.game.canvas.height * 0.9); | ||
} | ||
|
||
move(delta: number, direction: Direction) { | ||
// Change position | ||
this.x += this.speed * delta * direction; | ||
|
||
// Make sure spaceship cannot leave world boundaries | ||
this.x = Phaser.Math.Clamp(this.x, this.width / 2, | ||
this.sceneWidth - this.width / 2); | ||
} | ||
} |
5965373
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍