github.com/atsaki/terraform@v0.4.3-0.20150919165407-25bba5967654/website/source/assets/javascripts/app/Engine.Point.Puller.js (about) 1 (function( 2 Engine, 3 Vector 4 ){ 5 6 Engine.Point.Puller = function(id, x, y, shapeSize){ 7 this.id = id; 8 9 this.shapeSize = shapeSize; 10 this.ref = new Vector(x, y); 11 12 this.pos = new Vector( 13 x * shapeSize.x, 14 y * shapeSize.y 15 ); 16 17 this.home = this.pos.clone(); 18 this.accel = Vector.coerce(this.accel); 19 this.vel = Vector.coerce(this.vel); 20 }; 21 22 Engine.Point.Puller.prototype = { 23 24 fillStyle: null, 25 defaultFillstyle: '#b976ff', 26 chasingFillstyle: '#ff6b6b', 27 28 radius: 1, 29 30 maxSpeed: 160, 31 maxForce: 50, 32 33 pos: { 34 x: 0, 35 y: 0 36 }, 37 38 accel: { 39 x: 0, 40 y: 0 41 }, 42 43 vel: { 44 x: 0, 45 y: 0 46 }, 47 48 aRad: 200, 49 50 safety: 0.25, 51 52 resize: function(){ 53 this.home.x = this.pos.x = this.ref.x * this.shapeSize.x; 54 this.home.y = this.pos.y = this.ref.y * this.shapeSize.y; 55 56 return this; 57 }, 58 59 update: function(engine){ 60 var target = Vector.coerce(engine.mouse), 61 distanceToMouse, toHome, mag, safety; 62 63 target.x += (this.shapeSize.x - engine.width) / 2; 64 target.y += (this.shapeSize.y - engine.height) / 2; 65 66 distanceToMouse = this.distanceTo(target); 67 68 this.accel.mult(0); 69 70 if (distanceToMouse < this.aRad) { 71 this._chasing = true; 72 this.toChase(target); 73 this.fillStyle = this.chasingFillstyle; 74 } else { 75 this._chasing = false; 76 this.fillStyle = this.defaultFillstyle; 77 } 78 79 this.toChase(this.home, this.maxForce / 2); 80 81 this.vel.add(this.accel); 82 this.pos.add( 83 Vector.mult(this.vel, engine.tick) 84 ); 85 86 toHome = Vector.sub(this.home, this.pos); 87 mag = toHome.mag(); 88 safety = this.aRad * (this.safety * 3); 89 if (mag > this.aRad - safety) { 90 toHome.normalize(); 91 toHome.mult(this.aRad - safety); 92 this.pos = Vector.sub(this.home, toHome); 93 } 94 95 target = null; 96 toHome = null; 97 return this; 98 }, 99 100 toChase: function(target, maxForce){ 101 var desired, steer, distance, mult, safety; 102 103 maxForce = maxForce || this.maxForce; 104 105 target = Vector.coerce(target); 106 desired = Vector.sub(target, this.pos); 107 distance = desired.mag(); 108 desired.normalize(); 109 110 safety = this.aRad * this.safety; 111 112 if (distance < safety) { 113 mult = Engine.map(distance, 0, safety, 0, this.maxSpeed); 114 } else if (distance > this.aRad - safety){ 115 mult = Engine.map(this.aRad - distance, 0, safety, 0, this.maxSpeed); 116 } else { 117 mult = this.maxSpeed; 118 } 119 120 desired.mult(mult); 121 122 steer = Vector.sub(desired, this.vel); 123 steer.limit(maxForce); 124 this.accel.add(steer); 125 126 target = null; 127 desired = null; 128 steer = null; 129 }, 130 131 draw: function(ctx, scale){ 132 ctx.fillStyle = this.fillStyle; 133 ctx.fillRect( 134 (this.pos.x - this.radius / 2) * scale >> 0, 135 (this.pos.y - this.radius / 2) * scale >> 0, 136 this.radius * scale, 137 this.radius * scale 138 ); 139 140 return this; 141 }, 142 143 distanceTo: function(target) { 144 var xd = this.home.x - target.x; 145 var yd = this.home.y - target.y; 146 return Math.sqrt(xd * xd + yd * yd ); 147 } 148 }; 149 150 })( 151 window.Engine, 152 window.Vector 153 );