import Game from '@/core/game'
import Unit from '@/classes/unit'

import { unit, add } from '../core/vector2'

let trainingSet = []
let recordTrainingSet = false

export default class PlayerUnit extends Unit {

    constructor (position) {
        super(position, 'red')

        Game.on('scenepointermove', ev => {
            this.lookAt([ ev.x, ev.y ])
        })

        Game.on('pointerdown', ev => {
            this.firing = true
            this.weapon.reset()
        })

        Game.on('pointerup', ev => {
            this.firing = false
        })

        const keyMap = [
            { key: 'KeyW', v: [0, -1]},
            { key: 'KeyD', v: [1, 0]},
            { key: 'KeyS', v: [0, 1]},
            { key: 'KeyA', v: [-1, 0]}
        ]
        Game.on('key', keys => {
            const accelerateOnKey = (result, {key, v}) => keys[ key ] ? add(result, v) : result
            this.acceleration = unit( keyMap.reduce(accelerateOnKey, [0, 0]) )
            this.crouch = keys['ShiftLeft']
        })

        Game.on('draw', () => {
            Game.emit('log', 'HP: ' + this.health)
        })

    }

    update (dt) {
        this.move(dt)
    }

}
