vue-shovel/src/util/usePlayer.ts

27 lines
737 B
TypeScript
Raw Normal View History

2023-02-10 12:58:09 +01:00
import { computed, reactive } from 'vue'
import { RECIPROCAL } from '../level/def'
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-10 15:45:04 +01:00
x: 16,
y: 10,
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 }
}