|
| 1 | +import { canvas, context, scene, bulletsPool, screen, mouse } from '../store/store'; |
| 2 | +import { skirmish } from '../scene/scenes'; |
| 3 | +import { startMouseEvent, setEvent } from '../utilis/mouse.service'; |
| 4 | +import { TYPE } from '../clans/Clans'; |
| 5 | + |
| 6 | +export function startAppManager(component) { |
| 7 | + |
| 8 | + |
| 9 | + var Pool = (function () { |
| 10 | + var create = function (type, size) { |
| 11 | + var obj = Object.create(def); |
| 12 | + obj.init(type, size); |
| 13 | + |
| 14 | + return obj; |
| 15 | + }; |
| 16 | + |
| 17 | + var def = |
| 18 | + { |
| 19 | + _type: null, |
| 20 | + _size: null, |
| 21 | + _pointer: null, |
| 22 | + _elements: null, |
| 23 | + |
| 24 | + init: function (type, size) { |
| 25 | + this._type = type; |
| 26 | + this._size = size; |
| 27 | + this._pointer = size; |
| 28 | + this._elements = []; |
| 29 | + |
| 30 | + var i = 0; |
| 31 | + var length = this._size; |
| 32 | + |
| 33 | + for (i; i < length; ++i) { |
| 34 | + this._elements[i] = this._type; |
| 35 | + } |
| 36 | + }, |
| 37 | + |
| 38 | + getElement: function () { |
| 39 | + if (this._pointer > 0) return this._elements[--this._pointer]; |
| 40 | + |
| 41 | + return null; |
| 42 | + }, |
| 43 | + |
| 44 | + disposeElement: function (obj) { |
| 45 | + this._elements[this._pointer++] = obj; |
| 46 | + } |
| 47 | + }; |
| 48 | + return { create: create }; |
| 49 | + }()); |
| 50 | + |
| 51 | + var screenWidth; |
| 52 | + var screenHeight; |
| 53 | + var doublePI = Math.PI; |
| 54 | + |
| 55 | + var bulletPool; |
| 56 | + |
| 57 | + var keySpace = false; |
| 58 | + |
| 59 | + window.getAnimationFrame = |
| 60 | + window.requestAnimationFrame || |
| 61 | + window.webkitRequestAnimationFrame || |
| 62 | + window.mozRequestAnimationFrame || |
| 63 | + window.oRequestAnimationFrame || |
| 64 | + window.msRequestAnimationFrame || |
| 65 | + function (callback) { |
| 66 | + window.setTimeout(callback, 16.6); |
| 67 | + }; |
| 68 | + |
| 69 | + window.onload = function () { |
| 70 | + |
| 71 | + const can = component.querySelector('canvas'); |
| 72 | + canvas.set(can); |
| 73 | + context.set(can.getContext('2d')); |
| 74 | + window.onresize(); |
| 75 | + keyboardInit(); |
| 76 | + startMouseEvent(); |
| 77 | + bulletInit(); |
| 78 | + sceneInit(); |
| 79 | + |
| 80 | + loop(); |
| 81 | + |
| 82 | + setEvent("mousedown", (e) => { |
| 83 | + const _x = mouse.getX(); |
| 84 | + const _y = mouse.getY(); |
| 85 | + const _scene = scene.get(); |
| 86 | + const playerClan = Array.from(_scene.clans).find(c => c.player === TYPE.PLAYER); |
| 87 | + |
| 88 | + switch (e.which) { |
| 89 | + case 1: |
| 90 | + if (playerClan) { |
| 91 | + Array.from(playerClan.units).forEach(_unit => { |
| 92 | + const isinRangeX = Math.abs(_x - _unit.pos.getX()) < _unit.width; |
| 93 | + const isInRangeY = Math.abs(_y - _unit.pos.getY()) < _unit.height; |
| 94 | + _unit.isSelected = false; |
| 95 | + playerClan.selectedUnits = []; |
| 96 | + if (isinRangeX && isInRangeY) { |
| 97 | + _unit.isSelected = true; |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + break; |
| 102 | + case 2: |
| 103 | + alert('Middle Mouse button pressed.'); |
| 104 | + break; |
| 105 | + case 3: |
| 106 | + if(playerClan){ |
| 107 | + Array.from(playerClan.units).forEach(_unit=>{ |
| 108 | + if(_unit.isSelected) |
| 109 | + _unit.goTo(_x,_y); |
| 110 | + }); |
| 111 | + } |
| 112 | + break; |
| 113 | + default: |
| 114 | + alert('You have a strange Mouse!'); |
| 115 | + } |
| 116 | + |
| 117 | + }); |
| 118 | + }; |
| 119 | + |
| 120 | + window.onresize = function () { |
| 121 | + const can = canvas.get(); |
| 122 | + if (!can) return; |
| 123 | + |
| 124 | + screenWidth = can.clientWidth; |
| 125 | + screenHeight = can.clientHeight; |
| 126 | + |
| 127 | + screen.setHeight(screenHeight); |
| 128 | + screen.setWidth(screenWidth); |
| 129 | + |
| 130 | + |
| 131 | + can.width = screenWidth; |
| 132 | + can.height = screenHeight; |
| 133 | + }; |
| 134 | + |
| 135 | + function keyboardInit() { |
| 136 | + window.onkeydown = function (e) { |
| 137 | + switch (e.keyCode) { |
| 138 | + case 32: |
| 139 | + case 75: |
| 140 | + keySpace = true; |
| 141 | + break; |
| 142 | + } |
| 143 | + e.preventDefault(); |
| 144 | + }; |
| 145 | + |
| 146 | + window.onkeyup = function (e) { |
| 147 | + switch (e.keyCode) { |
| 148 | + //key Space |
| 149 | + case 75: |
| 150 | + case 32: |
| 151 | + keySpace = false; |
| 152 | + break; |
| 153 | + } |
| 154 | + |
| 155 | + e.preventDefault(); |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + function bulletInit() { |
| 160 | + bulletPool = Pool.create(null, 40); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + function sceneInit() { |
| 165 | + const _scene = skirmish(); |
| 166 | + scene.set(_scene); |
| 167 | + } |
| 168 | + |
| 169 | + function loop() { |
| 170 | + |
| 171 | + updateUnit(); |
| 172 | + updateBullets(); |
| 173 | + render(); |
| 174 | + getAnimationFrame(loop); |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + function updateUnit() { |
| 179 | + |
| 180 | + const _scene = scene.get(); |
| 181 | + Array.from(_scene.clans).forEach(_clan => { |
| 182 | + Array.from(_clan.units).forEach(_unit => { |
| 183 | + _unit.update(); |
| 184 | + const curX = _unit.pos.getX(); |
| 185 | + const curY = _unit.pos.getY(); |
| 186 | + // _unit.pos.setX( _unit.pos.getX()+1); |
| 187 | + if (_unit.angle < _unit.destAngle) |
| 188 | + _unit.angle += 5; |
| 189 | + else |
| 190 | + _unit.angle -= 5; |
| 191 | + |
| 192 | + if (keySpace) _unit.shoot(generateShot); |
| 193 | + |
| 194 | + if(_unit.moveX && _unit.moveX < curX){ |
| 195 | + _unit.x -= _unit.xStep; |
| 196 | + _unit.pos.setX(_unit.x); |
| 197 | + |
| 198 | + }else if(_unit.moveX){ |
| 199 | + _unit.x += _unit.xStep; |
| 200 | + _unit.pos.setX(_unit.x); |
| 201 | + } |
| 202 | + if(_unit.moveY && _unit.moveY < curY){ |
| 203 | + _unit.y -= _unit.yStep; |
| 204 | + _unit.pos.setY(_unit.y); |
| 205 | + }else if(_unit.moveX){ |
| 206 | + _unit.y += _unit.yStep; |
| 207 | + _unit.pos.setY(_unit.y); |
| 208 | + } |
| 209 | + |
| 210 | + }); |
| 211 | + }); |
| 212 | + } |
| 213 | + |
| 214 | + function updateBullets() { |
| 215 | + const bullets = bulletsPool.get(); |
| 216 | + var i = bullets.length - 1; |
| 217 | + |
| 218 | + for (i; i > -1; --i) { |
| 219 | + var b = bullets[i]; |
| 220 | + |
| 221 | + if (b.blacklisted) { |
| 222 | + b.reset(); |
| 223 | + |
| 224 | + bullets.splice(bullets.indexOf(b), 1); |
| 225 | + bulletPool.disposeElement(b); |
| 226 | + |
| 227 | + continue; |
| 228 | + } |
| 229 | + |
| 230 | + b.update(); |
| 231 | + |
| 232 | + if (b.pos.getX() > screenWidth) b.blacklisted = true; |
| 233 | + else if (b.pos.getX() < 0) b.blacklisted = true; |
| 234 | + |
| 235 | + if (b.pos.getY() > screenHeight) b.blacklisted = true; |
| 236 | + else if (b.pos.getY() < 0) b.blacklisted = true; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + function render() { |
| 241 | + const ctx = context.get(); |
| 242 | + const _scene = scene.get(); |
| 243 | + ctx.fillStyle = '#262626'; |
| 244 | + ctx.globalAlpha = 0.4; |
| 245 | + ctx.fillRect(0, 0, screenWidth, screenHeight); |
| 246 | + ctx.globalAlpha = 1; |
| 247 | + |
| 248 | + renderUnit(ctx,_scene); |
| 249 | + renderBullets(ctx); |
| 250 | + } |
| 251 | + |
| 252 | + function renderUnit(ctx,_scene) { |
| 253 | + |
| 254 | + Array.from(_scene.clans).forEach(_clan => { |
| 255 | + Array.from(_clan.units).forEach(_unit => { |
| 256 | + |
| 257 | + ctx.save(); |
| 258 | + ctx.translate(_unit.pos.getX() >> 0, _unit.pos.getY() >> 0); |
| 259 | + ctx.rotate((_unit.angle) * (Math.PI/180)); |
| 260 | + |
| 261 | + ctx.strokeStyle = '#FFF'; |
| 262 | + ctx.lineWidth = (Math.random() > 0.9) ? 2 : 1; |
| 263 | + ctx.beginPath(); |
| 264 | + ctx.moveTo(10, 0); |
| 265 | + ctx.lineTo(-10, -10); |
| 266 | + ctx.lineTo(-10, 10); |
| 267 | + ctx.lineTo(10, 0); |
| 268 | + ctx.stroke(); |
| 269 | + ctx.closePath(); |
| 270 | + ctx.restore(); |
| 271 | + if(_unit.isSelected){ |
| 272 | + ctx.beginPath(); |
| 273 | + ctx.arc(_unit.pos.getX(), _unit.pos.getY(), 20, 0, 20); |
| 274 | + ctx.stroke(); |
| 275 | + ctx.closePath(); |
| 276 | + ctx.restore(); |
| 277 | + |
| 278 | + } |
| 279 | + }); |
| 280 | + }); |
| 281 | + |
| 282 | + } |
| 283 | + |
| 284 | + function renderBullets(ctx) { |
| 285 | + const bullets = bulletsPool.get(); |
| 286 | + var i = bullets.length - 1; |
| 287 | + |
| 288 | + for (i; i > -1; --i) { |
| 289 | + var b = bullets[i]; |
| 290 | + |
| 291 | + ctx.beginPath(); |
| 292 | + ctx.strokeStyle = b.color; |
| 293 | + |
| 294 | + ctx.arc(b.pos.getX() >> 0, b.pos.getY() >> 0, b.radius, 0, doublePI); |
| 295 | + if (Math.random() > 0.4) ctx.stroke(); |
| 296 | + ctx.closePath(); |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + function generateShot() { |
| 301 | + |
| 302 | + const _scene = scene.get(); |
| 303 | + const bullets = bulletsPool.get(); |
| 304 | + Array.from(_scene.clans).forEach(_clan => { |
| 305 | + Array.from(_clan.units).forEach(_unit => { |
| 306 | + Array.from(_unit.weapon.bullets).forEach(_bullet => { |
| 307 | + var b = _bullet; |
| 308 | + b.radius = 1; |
| 309 | + b.pos.setX(_unit.pos.getX() + Math.cos(_unit.angle) * 14); |
| 310 | + b.pos.setY(_unit.pos.getY() + Math.sin(_unit.angle) * 14); |
| 311 | + b.vel.setLength(10); |
| 312 | + b.vel.setAngle((_unit.angle) * (Math.PI/180)); |
| 313 | + bullets[bullets.length] = b; |
| 314 | + }); |
| 315 | + }); |
| 316 | + }); |
| 317 | + |
| 318 | + |
| 319 | + } |
| 320 | +} |
| 321 | + |
0 commit comments