Path: blob/main/public/games/files/garbage-collector/js/input.js
1036 views
/*globals define*/1define([2'utils'3], function( Utils ) {4'use strict';56function Input() {7this.mouse = {8x: 0,9y: 0,1011down: false12};1314this.controls = {15TOP: false,16RIGHT: false,17BOTTOM: false,18LEFT: false19};2021this.keys = [];22this.game = null;2324this.touches = [];2526this.initialTouch = null;27this.touch = null;2829// Pixel radius where touch difference does nothing.30this.deadzone = 20;31// Maximum radius for touch movement.32this.touchLimit = 100;33}3435Input.prototype = {36onKeyDown: function( event ) {37this.keys[ event.which ] = true;3839// Arrow keys.40if ( event.which === 37 ||41event.which === 38 ||42event.which === 39 ||43event.which === 40 ) {44event.preventDefault();45}46},4748onKeyUp: function( event ) {49this.keys[ event.which ] = false;50},5152/**53* Accounts for difference between canvas dimensions and54* computed CSS-specified dimensions, as well as any canvas offsets.55*/56normalizeTouch: function( event ) {57var x = event.pageX,58y = event.pageY;5960if ( this.game ) {61var canvasWidth = this.game.canvas.width,62canvasHeight = this.game.canvas.height;6364var computedStyle = window.getComputedStyle( this.game.canvas );6566var computedWidth = parseInt( computedStyle.width, 10 ),67computedHeight = parseInt( computedStyle.height, 10 );6869var xRatio = canvasWidth / computedWidth,70yRatio = canvasHeight / computedHeight;7172x -= this.game.canvas.offsetLeft;73y -= this.game.canvas.offsetTop;7475x *= xRatio;76y *= yRatio;77}7879return {80pageX: x,81pageY: y82};83},8485onTouchStart: function( event ) {86this.touches = [].slice.call( event.touches );87if ( !this.initialTouch ) {88this.initialTouch = this.normalizeTouch( this.touches[0] );89}90},9192onTouchMove: function( event ) {93event.preventDefault();94this.touches = [].slice.call( event.touches );95this.touch = this.normalizeTouch( this.touches[0] );96},9798onTouchEnd: function( event ) {99this.touches = [].slice.call( event.touches );100if ( !this.touches.length ) {101this.initialTouch = null;102this.touch = null;103}104},105106update: function( dt ) {107var controls = this.controls;108109Object.keys( controls ).forEach(function( control ) {110controls[ control ] = false;111});112113// Keyboard update.114// Arrow keys.115if ( this.keys[ 37 ] ) { controls.LEFT = true; }116if ( this.keys[ 38 ] ) { controls.TOP = true; }117if ( this.keys[ 39 ] ) { controls.RIGHT = true; }118if ( this.keys[ 40 ] ) { controls.BOTTOM = true; }119120// WASD.121if ( this.keys[ 65 ] ) { controls.LEFT = true; }122if ( this.keys[ 87 ] ) { controls.TOP = true; }123if ( this.keys[ 68 ] ) { controls.RIGHT = true; }124if ( this.keys[ 83 ] ) { controls.BOTTOM = true; }125126this.updatePlayer( dt );127},128129updatePlayer: function( dt ) {130if ( this.game ) {131var controls = this.game.input.controls;132133var ax = 0,134ay = 0;135136if ( controls.LEFT ) { ax -= 800; }137if ( controls.RIGHT ) { ax += 800; }138if ( controls.TOP ) { ay -= 800; }139if ( controls.BOTTOM ) { ay += 800; }140141if ( this.touch ) {142var x = this.touch.pageX,143y = this.touch.pageY;144145var xi = this.initialTouch.pageX,146yi = this.initialTouch.pageY;147148var dx = x - xi,149dy = y - yi;150151var dz = this.deadzone;152153var angle;154// Parameters of touch extents.155var xt, yt;156157if ( Utils.distanceSquared( x, y, xi, yi ) > dz * dz ) {158angle = Math.atan2( dy, dx );159160// Subtract deadzone.161dx -= dz * Math.cos( angle );162dy -= dz * Math.sin( angle );163164// Get parameter up to touch limit.165xt = Utils.inverseLerp( Math.abs( dx ), 0, this.touchLimit );166yt = Utils.inverseLerp( Math.abs( dy ), 0, this.touchLimit );167168xt = Utils.clamp( xt, 0, 1 );169yt = Utils.clamp( yt, 0, 1 );170171// Determine sign.172xt *= ( dx < 0 ? -1 : 1 );173yt *= ( dy < 0 ? -1 : 1 );174175ax = xt * 800;176ay = yt * 800;177}178}179180// Move along camera direction.181var camera = this.game.camera;182if ( camera.angle ) {183var cos = Math.cos( -camera.angle ),184sin = Math.sin( -camera.angle );185186var rax = cos * ax - sin * ay,187ray = sin * ax + cos * ay;188189ax = rax;190ay = ray;191}192193this.game.player.accelerate( ax * dt, ay * dt );194}195},196197draw: function( ctx ) {198if ( this.game && this.initialTouch ) {199var x = this.initialTouch.pageX,200y = this.initialTouch.pageY;201202ctx.lineWidth = 8;203ctx.strokeStyle = 'rgba(0, 0, 0, 0.2)';204205ctx.beginPath();206ctx.arc( x, y, this.deadzone, 0, Utils.PI2 );207208ctx.moveTo( x + this.touchLimit, y );209ctx.arc( x, y, this.touchLimit, 0, Utils.PI2 );210211ctx.moveTo( x + this.deadzone + this.touchLimit, y );212ctx.arc( x, y, this.deadzone + this.touchLimit, 0, Utils.PI2 );213ctx.stroke();214}215}216};217218return Input;219});220221222