Skip to content

Commit

Permalink
chore: randomize enemy spawn and speed
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 21, 2025
1 parent cdff9ac commit 2852890
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/gameobjects/enemy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Sprite, Tag } from '../constants'
import type { Player } from '../types'

const ENEMY_SPEED = 200
const ENEMY_SPEED_MIN = 100
const ENEMY_SPEED_MAX = 300

export function addEnemy(x: number, y: number, player: Player) {
const enemy = add([
Expand All @@ -16,7 +17,7 @@ export function addEnemy(x: number, y: number, player: Player) {

enemy.onUpdate(() => {
const dir = player.pos.sub(enemy.pos).unit()
enemy.move(dir.scale(ENEMY_SPEED))
enemy.move(dir.scale(rand(ENEMY_SPEED_MIN, ENEMY_SPEED_MAX)))
})

enemy.onHurt(() => {
Expand Down
26 changes: 23 additions & 3 deletions src/scenes/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,28 @@ scene(Scene.Game, () => {
add([text('Press arrow keys', { width: width() / 2 }), pos(12, 12)])

loop(5, () => {
const x = rand(0, width())
const y = rand(0, height())
addEnemy(x, y, player)
if (coinflip()) {
// right
addEnemy(width(), rand(0, height()), player)
}

if (coinflip()) {
// left
addEnemy(-32, rand(0, height()), player)
}

if (coinflip()) {
// top
addEnemy(rand(0, width()), height(), player)
}

if (coinflip()) {
// bottom
addEnemy(rand(0, width()), -32, player)
}
})
})

function coinflip() {
return Boolean(randi(2))
}

0 comments on commit 2852890

Please sign in to comment.