27 lines
703 B
TypeScript
27 lines
703 B
TypeScript
![]() |
import { computed, reactive } from 'vue'
|
||
|
import { RECIPROCAL } from '../level/def'
|
||
|
|
||
|
export interface Moveable {
|
||
|
x: number, // position on x-axis (always 0 for the player)
|
||
|
y: number, // position on y-axis (always 0 for the player)
|
||
|
lastDir: number, // store last face direction
|
||
|
vx: number, // velocity on the x-axis
|
||
|
vy: number, // velocity on the y-axis
|
||
|
}
|
||
|
|
||
|
const player = reactive({
|
||
|
x: 0,
|
||
|
y: 0,
|
||
|
lastDir: 0,
|
||
|
vx: 0,
|
||
|
vy: 0,
|
||
|
})
|
||
|
|
||
|
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 }
|
||
|
}
|