Skip to content

Commit

Permalink
Centralize initial game state
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrettgilliam committed Nov 23, 2024
1 parent 8e1649a commit c2f3b6b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/scripts/entities/apple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export class Apple implements Drawable {
private readonly game: Game;
private readonly body: Rectangle[];

constructor(game: Game, position?: PointData) {
constructor(game: Game, position: PointData) {
this.game = game;
this.position = position ? Point.fromJSON(position) : new Point(14, 9);
this.position = Point.fromJSON(position);
this.body = [
new Rectangle(this.game, () => this.position.x + 0 / 3, () => this.position.y + 1 / 3, 1 / 3, 1 / 3),
new Rectangle(this.game, () => this.position.x + 1 / 3, () => this.position.y + 0 / 3, 1 / 3, 1 / 3),
Expand Down
10 changes: 2 additions & 8 deletions src/scripts/entities/snake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,15 @@ export class Snake implements Drawable {
private newVelocityQueue: Point[];
private dying = false;

constructor(game: Game, bodyPoints?: PointData[]) {
constructor(game: Game, bodyPoints: PointData[]) {
this.game = game;

this.nextUpdateTime = 0;
this.velocity = Direction.None;
this.newVelocityQueue = [];
this.body = [];

if (bodyPoints) {
bodyPoints.forEach(p => this.grow(Point.fromJSON(p)));
} else {
this.grow(new Point(4, 9));
this.grow(new Point(3, 9));
this.grow(new Point(2, 9));
}
bodyPoints.forEach(p => this.grow(Point.fromJSON(p)));
}

get head() {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/enums/difficulty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const Difficulty = {

export type Difficulty = keyof typeof Difficulty;

export const DefaultDifficulty = 'Medium';
export const DEFAULT_DIFFICULTY = 'Medium';
14 changes: 7 additions & 7 deletions src/scripts/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as constants from './constants.ts';
import { StartMenu } from './menus/start-menu.ts';
import { PauseMenu } from './menus/pause-menu.ts';
import { GameOverMenu } from './menus/game-over-menu.ts';
import { DefaultDifficulty, Difficulty } from './enums/difficulty.ts';
import { DEFAULT_DIFFICULTY, Difficulty } from './enums/difficulty.ts';
import { SaveData } from './interfaces/save-data.ts';
import { SAVE_DATA_STORAGE_KEY } from './constants.ts';

Expand All @@ -20,7 +20,7 @@ export class Game implements Drawable {
public readonly background: string;
public gameState: GameState;
public score = 0;
public difficulty: Difficulty = DefaultDifficulty;
public difficulty: Difficulty = DEFAULT_DIFFICULTY;
public unitWidth = 0;
public apple!: Apple;
public snake!: Snake;
Expand Down Expand Up @@ -94,12 +94,12 @@ export class Game implements Drawable {
return true;
}

reset(data?: SaveData) {
this.score = (data?.score) || 0;
this.difficulty = (data?.difficulty) || this.difficulty;
reset(data: SaveData) {
this.score = data.score;
this.difficulty = data.difficulty;
this.lastTouchTime = 0;
this.apple = new Apple(this, data?.apple.position);
this.snake = new Snake(this, data?.snake.body);
this.apple = new Apple(this, data.apple.position);
this.snake = new Snake(this, data.snake.body);
}

onresize() {
Expand Down
17 changes: 16 additions & 1 deletion src/scripts/interfaces/save-data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Difficulty } from '../enums/difficulty.ts';
import { DEFAULT_DIFFICULTY, Difficulty } from '../enums/difficulty.ts';
import { PointData } from './point-data.ts';

export interface SaveData {
Expand All @@ -11,3 +11,18 @@ export interface SaveData {
body: PointData[];
};
}

export const InitialSaveData: SaveData = {
score: 0,
difficulty: DEFAULT_DIFFICULTY,
apple: {
position: { x: 14, y: 9 }
},
snake: {
body: [
{ x: 4, y: 9 },
{ x: 3, y: 9 },
{ x: 2, y: 9 }
]
}
};
7 changes: 4 additions & 3 deletions src/scripts/menus/start-menu.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { MenuBase } from './menu-base.ts';
import { CanvasLabel } from '../controls/canvas-label.ts';
import { DefaultDifficulty, Difficulty } from '../enums/difficulty.ts';
import { Difficulty } from '../enums/difficulty.ts';
import { CanvasButton } from '../controls/canvas-button.ts';
import { GameState } from '../enums/game-state.ts';
import { Game } from '../game.ts';
import { InitialSaveData } from '../interfaces/save-data.ts';

export class StartMenu extends MenuBase {

Expand All @@ -22,11 +23,11 @@ export class StartMenu extends MenuBase {
}

get defaultButtonIndex() {
return this.buttons.findIndex(b => b.text() === DefaultDifficulty);
return this.buttons.findIndex(b => b.text() === this.game.difficulty);
}

setupGame() {
this.game.reset();
this.game.reset(InitialSaveData);
this.game.difficulty = this.buttons[this.buttonIndex].text() as Difficulty;
}
}

0 comments on commit c2f3b6b

Please sign in to comment.