Skip to content

Commit

Permalink
Move boat to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinten committed Aug 19, 2024
1 parent 68899b7 commit f837521
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
21 changes: 21 additions & 0 deletions game/boat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from './js/draw/path.js';

let d = 'M28,10C23.46,7.85,11.54,4.43,8,8c-4.22,4.26-4.85,20.46,0,24c4.06,2.96,15.7,0.6,20-2c3.03-1.83,10-7.99,10-10 S31.19,11.51,28,10z M14,29.04V10.74c1.13-0.23,2.54-0.19,4,0.01v18.39C16.55,29.29,15.16,29.27,14,29.04z M25.94,26.9 c-0.51,0.31-1.18,0.61-1.94,0.89V12.26c0.75,0.27,1.42,0.54,1.94,0.79c0.56,0.27,1.29,0.76,2.06,1.36V25.4 C27.22,26.04,26.49,26.57,25.94,26.9z';

export default (options = {}) => {
let defaults = {
paths: [d],
x: 0,
y: 0,
w: 40,
h: 40,
a: 0,
fills: ['Coral'],
gx: 0,
gy: 0
};
Object.assign(defaults, options);
Object.assign(options, defaults);
let boat = path(options);
return boat;
};
3 changes: 0 additions & 3 deletions game/d.js

This file was deleted.

15 changes: 6 additions & 9 deletions game/level.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import machine from './js/statemachine/machine.js';
import state from './js/statemachine/state.js';
import ft from './js/draw/text.js';
import d from './d.js';
import path from './js/draw/path.js';
import createBoat from './boat.js';

let level = state();

Expand Down Expand Up @@ -205,7 +204,7 @@ level.on('start', () => {
for (let x = 0; x < nCols; x++) {
if (grid[y][x] === 0) {
let angle = angles[Math.floor(Math.random() * angles.length)];
let boat = path({paths: [d.b], x: x * 40, y: y * 40, fills: ['Seashell'], w: 40, h: 40, a: angle, gx: x, gy: y});
let boat = createBoat({x: x * 40, y: y * 40, a: angle, gx: x, gy: y});
boats.push(boat);
grid[y][x] = 1;
}
Expand Down Expand Up @@ -246,19 +245,18 @@ level.on('start', () => {
boat.y = ny * 40;
boat.gx = nx;
boat.gy = ny;
boat.fills = ['Seashell'];
grid[ny][nx] = 1;
grid[gy][gx] = 0;
grid[gy][gx] = grid[gy][gx] === 14 ? 13 : 0;
}
if (grid[ny][nx] === 13) {
boat.x = nx * 40;
boat.y = ny * 40;
boat.gx = nx;
boat.gy = ny;
boat.fills = ['Coral'];
grid[ny][nx] = 14;
grid[gy][gx] = 0;
grid[gy][gx] = grid[gy][gx] === 14 ? 13 : 0;
}
boat.fills = grid[boat.gy][boat.gx] === 14 ? ['Coral'] : ['Seashell'];
});

level.on('tap', e => {
Expand Down Expand Up @@ -305,7 +303,6 @@ level.on('start', () => {
boat.y = ny * 40;
boat.gx = nx;
boat.gy = ny;
boat.fills = ['Seashell'];
grid[ny][nx] = 1;
grid[gy][gx] = grid[gy][gx] === 14 ? 13 : 0;
} else if (grid[ny][nx] === 13) {
Expand All @@ -315,13 +312,13 @@ level.on('start', () => {
boat.y = ny * 40;
boat.gx = nx;
boat.gy = ny;
boat.fills = ['Coral'];
grid[ny][nx] = 14;
grid[gy][gx] = grid[gy][gx] === 14 ? 13 : 0;
} else {
// rotate the boat 180 degrees
boat.a += 180;
}
boat.fills = grid[boat.gy][boat.gx] === 14 ? ['Coral'] : ['Seashell'];
}
if (value === 14) {
// check if there are no boats left on the coral reef
Expand Down
5 changes: 2 additions & 3 deletions game/menu.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import machine from './js/statemachine/machine.js';
import state from './js/statemachine/state.js';
import ft from './js/draw/text.js';
import d from './d.js';
import path from './js/draw/path.js';
import createBoat from './boat.js';
import tappable from './js/pointer/rect.js';

let menu = state();

let boat = path({paths: [d.b], x: 0, y: 0, fills: ['Coral'], w: 40, h: 40});
let boat = createBoat();
tappable(boat);

menu.on('start', () => {
Expand Down

0 comments on commit f837521

Please sign in to comment.