Path: blob/main/public/games/files/garbage-collector/js/effects/trigger-wire.js
1036 views
/*globals define*/1define([2'base-object',3'config/colors',4'config/material',5'config/settings',6'utils'7], function( BaseObject, Colors, Material, Settings, Utils ) {8'use strict';910var Direction = {11TOP: 0,12LEFT: 1,13BOTTOM: 2,14RIGHT: 315};1617var defaults = {18sourceDirection: Direction.TOP,19targetDirection: Direction.TOP,2021// Relative coordinates.22vertices: []23};2425function TriggerWire( source, target, options ) {26BaseObject.call( this );2728// Trigger.29this.source = source;30// Door.31this.target = target;3233Utils.defaults( this, options, defaults );34}3536TriggerWire.prototype = new BaseObject();37TriggerWire.prototype.constructor = TriggerWire;3839TriggerWire.prototype.update = function() {};4041TriggerWire.prototype.draw = function( ctx ) {42if ( !this.source || !this.target ) {43return;44}4546var x0 = this.source.x,47y0 = this.source.y,48r0 = this.source.radius;4950var frameRatio = this.source.frameRatio,51frameRadius = frameRatio * r0;5253var x1 = this.target.x,54y1 = this.target.y,55r1 = this.target.radius;5657// Calculate endpoint locations.58var horizontal = this.sourceDirection % 2,59sign = this.sourceDirection < 2 ? -1 : 1;6061x0 += horizontal * sign * frameRadius;62y0 += !horizontal * sign * frameRadius;6364horizontal = this.targetDirection % 2;65sign = this.targetDirection < 2 ? -1 : 1;6667x1 += horizontal * sign * r1;68y1 += !horizontal * sign * r1;6970var glowColor = Colors.Glow[ Material.type( this.source.material ) ];7172// Draw wire.73var dx = x1 - x0,74dy = y1 - y0;7576ctx.beginPath();77ctx.moveTo( x0, y0 );7879var xi, yi;80for ( var i = 0, il = 0.5 * this.vertices.length; i < il; i++ ) {81xi = this.vertices[ 2 * i ];82yi = this.vertices[ 2 * i + 1 ];83ctx.lineTo( x0 + xi * dx, y0 + yi * dy );84}8586ctx.lineTo( x1, y1 );8788if ( this.source.active && glowColor ) {89ctx.strokeStyle = glowColor;90} else {91ctx.strokeStyle = '#333';92}9394if ( Settings.glow ) {95ctx.globalCompositeOperation = 'lighter';96}9798ctx.lineWidth = 0.2 * r0;99ctx.stroke();100101if ( Settings.glow ) {102ctx.globalCompositeOperation = 'source-over';103}104105// Draw endpoints.106ctx.beginPath();107ctx.rect( -0.2 * r0 + x0, -0.2 * r0 + y0, 0.4 * r0, 0.4 * r0 );108ctx.fillStyle = '#fff';109ctx.fill();110111if ( this.source.active && glowColor ) {112ctx.strokeStyle = glowColor;113} else {114ctx.strokeStyle = '#333';115}116117ctx.lineWidth = 0.1 * r0;118ctx.stroke();119120ctx.beginPath();121ctx.arc( x1, y1, 0.2 * r1, 0, Utils.PI2 );122ctx.fillStyle = '#fff';123ctx.fill();124125ctx.lineWidth = 0.1 * r1;126ctx.stroke();127};128129// This doesn't handle negative vertex values.130TriggerWire.prototype.aabb = function() {131if ( !this.source || !this.target ) {132return null;133}134135var x0 = this.source.x,136y0 = this.source.y,137r0 = this.source.radius;138139var x1 = this.target.x,140y1 = this.target.y;141142var aabb = {143xmin: Math.min( x0, x1 ),144ymin: Math.min( y0, y1 ),145xmax: Math.max( x0, x1 ),146ymax: Math.max( y0, y1 )147};148149// AABB radius.150var radius = 0.1 * r0;151return Utils.relativeExpandAABB( aabb, radius, radius );152};153154TriggerWire.Direction = Direction;155156return TriggerWire;157});158159160