vue-shovel/src/util/usePlayer.ts

27 lines
803 B
TypeScript
Raw Normal View History

2023-02-10 12:58:09 +01:00
import { computed, reactive } from 'vue'
2023-02-12 20:36:51 +01:00
import { RECIPROCAL, STAGE_WIDTH, STAGE_HEIGHT } from '../level/def'
2023-02-10 12:58:09 +01:00
export interface Moveable {
2023-02-10 15:45:04 +01:00
x: number, // position on x-axis (fixed for the player)
y: number, // position on y-axis (fixed for the player)
2023-02-10 12:58:09 +01:00
lastDir: number, // store last face direction
vx: number, // velocity on the x-axis
vy: number, // velocity on the y-axis
}
const player = reactive({
2023-02-12 20:36:51 +01:00
x: (STAGE_WIDTH + 2) / 2,
y: (STAGE_HEIGHT + 2) / 2,
2023-02-10 12:58:09 +01:00
lastDir: 0,
vx: 0,
2023-02-10 15:45:04 +01:00
vy: 1, // always falling, because of gravity
2023-02-10 12:58:09 +01:00
})
export default function usePlayer() {
const dx = computed(() => player.vx * RECIPROCAL)
const dy = computed(() => player.vy * RECIPROCAL)
const direction = computed(() => (player.lastDir < 0 ? 'left' : 'right'))
return { player, direction, dx, dy }
}