import { PolusBuffer } from "../../util/polusBuffer" ; export interface Position { X : number ; Y : number ; } export class Vector2 { X : number ; Y : number ; constructor ( X = - 1 , Y = - 1 ) { this . X = X ; this . Y = Y ; } parse ( buffer ) { this . X = this . lerp ( - 40 , 40 , buffer . readU16 ( ) / 65535 ) ; this . Y = this . lerp ( - 40 , 40 , buffer . readU16 ( ) / 65535 ) ; return this ; } serialize ( ) { var buf = new PolusBuffer ( 4 ) ; buf . writeU16 ( this . unlerp ( - 40 , 40 , this . X ) * 65535.0 ) ; buf . writeU16 ( this . unlerp ( - 40 , 40 , this . Y ) * 65535.0 ) ; return buf ; } private lerp ( min , max , value ) { if ( value < 0 ) { value = 0 ; } else if ( value > 1 ) { value = 1 ; } return min + ( max - min ) * value ; } private unlerp ( min , max , value ) { var res = ( value - min ) / ( max - min ) ; if ( res < 0 ) { res = 0 ; } else if ( res > 1 ) { res = 1 ; } return res ; } }