Path: blob/main/public/games/files/garbage-collector/js/entities/trigger.js
1036 views
/*jshint camelcase: false*/1/*globals define*/2define([3'entities/physics-entity',4'config/colors',5'config/material',6'config/settings',7'utils'8], function( PhysicsEntity, Colors, Material, Settings, Utils ) {9'use strict';1011var HALF_PI = Utils.HALF_PI;1213var defaults = {14duration: 0.4,1516// So we know the actual drawn size.17frameRatio: 1.2,1819platformAngle: 0,20platformAngularVelocity: 221};2223function Trigger( x, y, radius, material, options ) {24this.radius = radius || 0;2526PhysicsEntity.call( this, {27shape: 'circle',28radius: this.radius,29fixture: {30isSensor: true,31filter: {32categoryBits: material33}34},35body: {36position: {37x: x,38y: y39}40}41});4243Utils.defaults( this, options, defaults );4445this.object = null;46// If there is an object AND the trigger has caught it.47this.active = false;4849this.time = 0;50}5152Trigger.prototype = new PhysicsEntity();53Trigger.prototype.constructor = Trigger;5455Trigger.prototype.update = function( dt ) {56PhysicsEntity.prototype.update.call( this, dt );5758// When the correct trash object enters the trigger,59// set its lifeTime to infinite.60if ( this.object && !this.active ) {61this.object.x = this.x;62this.object.y = this.y;63this.object.vx = 0;64this.object.vy = 0;65this.object.fixture.SetSensor( true );66if ( typeof this.object.lifeTime !== 'undefined' ) {67this.object.lifeTime = Number.POSITIVE_INFINITY;68}6970this.active = true;7172// Super hacky last minute audio.73var sound = document.getElementById( 'trigger-activate-audio' );74if ( sound ) {75sound.volume = 0.1;76sound.load();77sound.play();78}79}8081// Opening animation.82if ( this.active && this.time < this.duration ) {83this.time += dt;84}8586// Rotating animation.87if ( this.active ) {88this.platformAngle += this.platformAngularVelocity * dt;89}90};9192Trigger.prototype.drawPath = function( ctx ) {93var radius = this.radius;9495var size = this.frameRatio * ( 2 * radius ),96halfSize = 0.5 * size,97quarterSize = 0.5 * halfSize;9899// Draw base.100ctx.beginPath();101ctx.rect( -halfSize, -halfSize, size, size );102ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';103ctx.fill();104105// Draw frame/border.106var glowColor = Colors.Glow[ Material.type( this.material ) ];107108if ( glowColor ) {109ctx.beginPath();110111// Top left.112ctx.moveTo( -quarterSize, -halfSize );113ctx.lineTo( -halfSize, -halfSize );114ctx.lineTo( -halfSize, -quarterSize );115116// Bottom left.117ctx.moveTo( -halfSize, quarterSize );118ctx.lineTo( -halfSize, halfSize );119ctx.lineTo( -quarterSize, halfSize );120121// Bottom right.122ctx.moveTo( quarterSize, halfSize );123ctx.lineTo( halfSize, halfSize );124ctx.lineTo( halfSize, quarterSize );125126// Top right.127ctx.moveTo( halfSize, -quarterSize );128ctx.lineTo( halfSize, -halfSize );129ctx.lineTo( quarterSize, -halfSize );130131ctx.lineWidth = 0.3 * radius;132ctx.strokeStyle = glowColor;133ctx.stroke();134135ctx.lineWidth = 0.1 * radius;136ctx.strokeStyle = '#fff';137ctx.stroke();138}139140var halfRadius = 0.5 * radius;141142var t = 1;143if ( this.time < this.duration ) {144t = this.time / this.duration;145}146147t *= 0.5 * halfRadius;148149if ( this.platformAngle ) {150ctx.save();151ctx.rotate( this.platformAngle );152}153154// Draw beams.155if ( glowColor && this.time >= this.duration ) {156if ( Settings.glow ) {157ctx.globalCompositeOperation = 'lighter';158}159160// 45 degrees.161var diagonal = 0.5 * Math.sqrt( 2 ) * halfRadius;162163ctx.beginPath();164// Left diagonal.165ctx.moveTo( -t - diagonal, -t - diagonal );166ctx.lineTo( t + diagonal, t + diagonal );167// Right diagonal.168ctx.moveTo( t + diagonal, -t - diagonal );169ctx.lineTo( -t - diagonal, t + diagonal );170171ctx.lineWidth = ( 0.3 + Math.random() * 0.2 ) * radius;172ctx.strokeStyle = glowColor;173ctx.stroke();174175ctx.lineWidth = 0.2 * radius;176ctx.strokeStyle = '#fff';177ctx.stroke();178179if ( Settings.glow ) {180ctx.globalCompositeOperation = 'source-over';181}182}183184// Draw sensor.185ctx.beginPath();186187// Top left.188ctx.moveTo( -halfRadius - t, -t );189ctx.arc( -t, -t, halfRadius, -Math.PI, -HALF_PI );190191// Top right.192ctx.moveTo( t, -halfRadius - t );193ctx.arc( t, -t, halfRadius, -HALF_PI, 0 );194195// Bottom right.196ctx.moveTo( halfRadius + t, t );197ctx.arc( t, t, halfRadius, 0, HALF_PI );198199// Bottom left.200ctx.moveTo( -t, halfRadius + t );201ctx.arc( -t, t, halfRadius, HALF_PI, Math.PI );202203ctx.lineWidth = 0.5 * radius;204ctx.strokeStyle = '#333';205ctx.stroke();206207ctx.lineWidth = 0.2 * radius;208ctx.strokeStyle = '#fff';209ctx.stroke();210211if ( this.platformAngle ) {212ctx.restore();213}214215PhysicsEntity.prototype.drawPath.call( this, ctx );216};217218Trigger.prototype.aabb = function() {219var aabb = PhysicsEntity.prototype.aabb.call( this );220// Add half lineWidth of frame.221var ratio = this.frameRatio + 0.15;222return Utils.ratioExpandAABB( aabb, ratio, ratio );223};224225return Trigger;226});227228229