import { SPEED } from '~/common/config' ; import { gameMap } from '~/main' ; export interface ObserverInterface { x : number ; y : number ; moveTo ( nx , ny ) : void ; update ( ) : void ; } export class Observer implements ObserverInterface { private vx : number = 0 ; private vy : number = 0 ; constructor ( public x , public y ) { } /**
     * stop moving
     */ public stop ( ) { this . vx = 0 ; this . vy = 0 ; } public moveTo ( nx , ny ) { const mx = gameMap . view . relativeX ( nx ) ; const my = gameMap . view . relativeY ( ny ) ; const ox = gameMap . view . relativeX ( this . x ) ; const oy = gameMap . view . relativeY ( this . y ) ; const xc = mx - ox ; const yc = my - oy ; const hyp = Math . sqrt ( xc * xc + yc * yc ) ; const ratio = SPEED * gameMap . scale / hyp ; this . vx = xc * ratio ; this . vy = yc * ratio ; } public update ( ) { this . x += this . vx ; this . y += this . vy ; gameMap . limit ( this ) ; } }