generated from remarkablegames/kaplay-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(helpers): modularize coinflip and outsideCoordinates
- Loading branch information
1 parent
aee14ab
commit 53a63fe
Showing
4 changed files
with
24 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Returns true or false. | ||
*/ | ||
export function coinflip() { | ||
return Boolean(randi(2)) | ||
} |
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,14 @@ | ||
import { coinflip } from './chance' | ||
|
||
/** | ||
* Generate random coordinates outside the camera view. | ||
*/ | ||
export function outsideCoordinates(x: number, y: number) { | ||
const halfWidth = width() / 2 | ||
const halfHeight = height() / 2 | ||
const multiplier = coinflip() ? 1 : -1 | ||
return { | ||
x: x + halfWidth * multiplier, | ||
y: y + halfHeight * multiplier, | ||
} | ||
} |
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,2 @@ | ||
export * from './chance' | ||
export * from './coordinates' |
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 |
---|---|---|
@@ -1,30 +1,14 @@ | ||
import { Scene } from '../constants' | ||
import { addEnemy, addPlayer } from '../gameobjects' | ||
import { outsideCoordinates } from '../helpers' | ||
|
||
scene(Scene.Game, () => { | ||
const player = addPlayer() | ||
|
||
add([text('Press arrow keys', { width: width() / 2 }), pos(12, 12)]) | ||
|
||
loop(5, () => { | ||
const { x, y } = coordinates(player.pos.x, player.pos.y) | ||
const { x, y } = outsideCoordinates(player.pos.x, player.pos.y) | ||
addEnemy(x, y, player) | ||
}) | ||
}) | ||
|
||
/** | ||
* Random coordinates outside the camera view. | ||
*/ | ||
function coordinates(x: number, y: number) { | ||
const halfWidth = width() / 2 | ||
const halfHeight = height() / 2 | ||
const multiplier = coinflip() ? 1 : -1 | ||
return { | ||
x: x + halfWidth * multiplier, | ||
y: y + halfHeight * multiplier, | ||
} | ||
} | ||
|
||
function coinflip() { | ||
return Boolean(randi(2)) | ||
} |