Last active
March 23, 2024 21:27
-
-
Save Rio6/3f0bb7d9ea4409733964262fc942ad6d to your computer and use it in GitHub Desktop.
Turns Istrolid into 3D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Turns Istrolid into 3D | |
| * by R26 | |
| */ | |
| var istrol3d = window.istrol3d || { | |
| GameMode_toGameSpace: GameMode.prototype.toGameSpace, | |
| GameMode_fromGameSpace: GameMode.prototype.fromGameSpace, | |
| BattleMode_reset: BattleMode.prototype.reset, | |
| BattleMode_onmousemove: BattleMode.prototype.onmousemove, | |
| BattleMode_onkeydown: BattleMode.prototype.onkeydown, | |
| Atlas_startFrame: Atlas.prototype.startFrame, | |
| Atlas_beginSprites: Atlas.prototype.beginSprites, | |
| }; | |
| // battle.js changes | |
| GameMode.prototype.toGameSpace = function(point) { | |
| var p; | |
| if (this.camera) { | |
| p = this.camera.unproject([point[0] / window.innerWidth * 2 - 1, -(point[1] / window.innerHeight * 2 - 1)]); | |
| return v3.lerp(p[0], p[1], -p[0][2] / (p[1][2] - p[0][2]), point); | |
| } | |
| return istrol3d.GameMode_toGameSpace.call(this, point); | |
| }; | |
| GameMode.prototype.fromGameSpace = function(point) { | |
| var p; | |
| if (this.camera) { | |
| p = this.camera.project([point[0], point[1]]); | |
| return [(p[0] + 1) / 2 * window.innerWidth, (-p[1] + 1) / 2 * window.innerHeight]; | |
| } | |
| return istrol3d.GameMode_fromGameSpace.call(this, point); | |
| }; | |
| _pos = v3.create(); | |
| _dir = v3.create(); | |
| _fwd = v3.create(); | |
| _mat = m4.create(); | |
| window.Camera = (function() { | |
| function Camera() { | |
| this.pos = v3.create([0, 0, 10000]); | |
| this.dir = v3.create(); | |
| this.yaw = Math.PI / 2 - 0.0001; | |
| this.pitch = -Math.PI / 2 + 0.0001; | |
| this.near = 50; | |
| this.far = 100000; | |
| this.fovy = 45; | |
| this.redoDir = true; | |
| this.followUnit = null; | |
| } | |
| Camera.prototype.rotate = function(yaw, pitch) { | |
| this.yaw += yaw; | |
| this.pitch += pitch; | |
| if (this.pitch <= -Math.PI / 2) { | |
| this.pitch = -Math.PI / 2 + 0.0001; | |
| } | |
| if (this.pitch >= Math.PI / 2) { | |
| this.pitch = Math.PI / 2 - 0.0001; | |
| } | |
| return this.redoDir = true; | |
| }; | |
| Camera.prototype.updateDir = function(globCoord) { | |
| var cosp, cosy, pitch, sinp, siny, yaw; | |
| if (globCoord == null) { | |
| globCoord = true; | |
| } | |
| if (this.redoDir || this.followUnit) { | |
| if (!globCoord && this.followUnit) { | |
| yaw = this.yaw + this.followUnit.rot - Math.PI / 2; | |
| } else { | |
| yaw = this.yaw; | |
| } | |
| pitch = this.pitch; | |
| siny = Math.sin(yaw); | |
| cosy = Math.cos(yaw); | |
| sinp = Math.sin(pitch); | |
| cosp = Math.cos(pitch); | |
| this.dir[0] = cosy * cosp; | |
| this.dir[1] = siny * cosp; | |
| this.dir[2] = sinp; | |
| return this.redoDir = false; | |
| } | |
| }; | |
| Camera.prototype.move = function(x, y, z) { | |
| if (z == null) { | |
| z = 0; | |
| } | |
| this.updateDir(true); | |
| v3.set(this.dir, _fwd); | |
| _fwd[2] = 0; | |
| v3.norm(_fwd); | |
| v3.add(this.pos, v3.scale(_fwd, y, _dir)); | |
| v3.add(this.pos, v3.scale(v3.cross(_fwd, Z, _fwd), x)); | |
| v3.add(this.pos, v3.scale(Z, z, _dir)); | |
| if (this.pos[2] < 0) { | |
| return this.pos[2] = 0; | |
| } | |
| }; | |
| Camera.prototype.follow = function(unit) { | |
| if (unit) { | |
| this.followUnit = unit; | |
| v2.zero(this.pos); | |
| this.pos[2] = 150; | |
| return this.yaw = 0; | |
| } else { | |
| return this.followUnit = null; | |
| } | |
| }; | |
| Camera.prototype.mat = function() { | |
| var rot; | |
| this.updateDir(false); | |
| if (this.followUnit) { | |
| rot = this.followUnit.rot - Math.PI / 2; | |
| v2.rotate(this.pos, rot, _pos); | |
| v2.add(_pos, this.followUnit.pos); | |
| _pos[2] = this.pos[2]; | |
| } else { | |
| v3.set(this.pos, _pos); | |
| } | |
| v3.add(_pos, this.dir, _dir); | |
| m4.lookAt(_pos, _dir, Z, _mat); | |
| m4.mul(m4.perspective(this.fovy, window.innerWidth / window.innerHeight, this.near, this.far), _mat, _mat); | |
| return _mat; | |
| }; | |
| Camera.prototype.project = function(point) { | |
| var p; | |
| p = v3.set(point, []); | |
| if (!p[2]) { | |
| p[2] = 0; | |
| } | |
| p[3] = 1; | |
| m4.mul_v4(this.mat(), p); | |
| return v3.scale(p, 1 / p[3], point); | |
| }; | |
| Camera.prototype.unproject = function(point) { | |
| var mat, p0, p1; | |
| mat = m4.inverse(this.mat(), []); | |
| p0 = v2.set(point, []); | |
| p0[2] = 0; | |
| p0[3] = 1; | |
| p1 = v2.set(point, []); | |
| p1[2] = 1; | |
| p1[3] = 1; | |
| m4.mul_v4(mat, p0); | |
| m4.mul_v4(mat, p1); | |
| return [v3.scale(p0, 1 / p0[3]), v3.scale(p1, 1 / p1[3])]; | |
| }; | |
| return Camera; | |
| })(); | |
| BattleMode.prototype.reset = function() { | |
| this.camera = new Camera(); | |
| return istrol3d.BattleMode_reset.call(this); | |
| }; | |
| BattleMode.prototype.onmousemove = function(e) { | |
| if (this.selecting) { | |
| this.selection.style.display = "block"; | |
| startx = Math.min(this.selectAt[0], e.clientX); | |
| starty = Math.min(this.selectAt[1], e.clientY); | |
| endx = Math.max(this.selectAt[0], e.clientX); | |
| endy = Math.max(this.selectAt[1], e.clientY); | |
| this.selection.style.left = startx + "px"; | |
| this.selection.style.top = starty + "px"; | |
| this.selection.style.width = -startx + endx + "px"; | |
| this.selection.style.height = -starty + endy + "px"; | |
| selected = []; | |
| if (v2.distance([startx, starty], [endx, endy]) < 10) { | |
| point = this.toGameSpace([startx, starty]); | |
| unit = this.closestUnit(point); | |
| if (unit) { | |
| selected.push(unit); | |
| } | |
| } else { | |
| ref = intp.things; | |
| for (id in ref) { | |
| thing = ref[id]; | |
| if (!thing.unit) { | |
| continue; | |
| } | |
| pos = this.fromGameSpace(thing.pos); | |
| if ((startx < (ref1 = pos[0]) && ref1 < endx) && (starty < (ref2 = pos[1]) && ref2 < endy)) { | |
| selected.push(thing); | |
| } | |
| } | |
| } | |
| if (this.selectAdd) { | |
| ref3 = commander.selection; | |
| for (j = 0, len = ref3.length; j < len; j++) { | |
| thing = ref3[j]; | |
| selected.push(thing); | |
| } | |
| } | |
| ifHasFilter = function(selected, fn) { | |
| var has, k, l, len1, len2, newSelected, t; | |
| has = false; | |
| for (k = 0, len1 = selected.length; k < len1; k++) { | |
| t = selected[k]; | |
| if (fn(t)) { | |
| has = true; | |
| } | |
| } | |
| if (!has) { | |
| return selected; | |
| } | |
| newSelected = []; | |
| for (l = 0, len2 = selected.length; l < len2; l++) { | |
| t = selected[l]; | |
| if (fn(t)) { | |
| newSelected.push(t); | |
| } | |
| } | |
| return newSelected; | |
| }; | |
| selected = (function() { | |
| var k, len1, results; | |
| results = []; | |
| for (k = 0, len1 = selected.length; k < len1; k++) { | |
| t = selected[k]; | |
| if (t.unit) { | |
| results.push(t); | |
| } | |
| } | |
| return results; | |
| })(); | |
| selected = ifHasFilter(selected, function(u) { | |
| return u.owner === commander.id; | |
| }); | |
| selected.sort(function(a, b) { | |
| return a.id - b.id; | |
| }); | |
| this.selectUnitsFake(selected); | |
| } | |
| if (this.moving) { | |
| dx = e.movementX; | |
| dy = e.movementY; | |
| if (dx === void 0) { | |
| dx = e.mozMovementX; | |
| } | |
| if (dy === void 0) { | |
| dy = e.mozMovementY; | |
| } | |
| this.camera.rotate(-dx / 1000 * Math.PI, -dy / 1000 * Math.PI); | |
| } | |
| let selecting = this.selecting; | |
| let moving = this.moving; | |
| this.selecting = false; | |
| this.moving = false; | |
| let ret = istrol3d.BattleMode_onmousemove.call(this, e); | |
| this.selecting = selecting; | |
| this.moving = moving; | |
| return ret; | |
| }; | |
| BattleMode.prototype.onzoom = function(delta, e) { | |
| var z; | |
| z = 10; | |
| return this.camera.move(0, 0, delta * z); | |
| }; | |
| BattleMode.prototype.controls = function() { | |
| this.zoom = 1 + this.camera.pos[2] / 1000; | |
| BattleMode.prototype.__proto__.controls.call(this); | |
| return this.camera.move(-this.keyScroll[0], -this.keyScroll[1], this.keyZoom * 1000); | |
| }; | |
| BattleMode.prototype.onkeydown = function(e) { | |
| if (settings.key(e, "Focus on Units")) { | |
| this.centerOnUnit = true; | |
| if (commander.selection.length > 0) { | |
| this.camera.follow(commander.selection[0]); | |
| } else { | |
| this.camera.follow(null); | |
| } | |
| } | |
| return istrol3d.BattleMode_onkeydown.call(this, e); | |
| } | |
| BattleMode.prototype.draw = function() { | |
| var bg_zoom, theme, z; | |
| baseAtlas.beginSprites([0, 0], 1, null, this.camera.mat()); | |
| if (this.follow) { | |
| control.setCursor("mouseAttack"); | |
| } else { | |
| control.setCursor("mouse"); | |
| } | |
| if (intp.theme != null) { | |
| theme = intp.theme; | |
| } else { | |
| theme = mapping.themes[0]; | |
| } | |
| bg_zoom = Math.max(window.innerWidth, window.innerHeight) / 120; | |
| z = bg_zoom * 5; | |
| baseAtlas.drawSprite("img/newbg/gradient.png", [0, 0], [z, z], 0, theme.spotColor, -100); | |
| intp.draw(); | |
| if (this.drawPoints.length > 0) { | |
| this.computeLineMove(); | |
| } | |
| return baseAtlas.finishSprites(); | |
| }; | |
| // Reset battle mode | |
| { | |
| let oldServer = battleMode.server; | |
| battleMode.reset(); // needed to prevent error when other's still referencing to old mode | |
| window.battleMode = new BattleMode(); | |
| battleMode.server = oldServer; | |
| } | |
| // engine.js changes | |
| baseAtlas.drawMargin = 0; | |
| baseAtlas.camera = m4.create(); | |
| gl.enable(gl.DEPTH_TEST); | |
| gl.depthFunc(gl.LEQUAL); | |
| var initShader = function() { | |
| var compileShader, linkShader, simpleFragment, simpleVertex; | |
| compileShader = function(type, src) { | |
| var shader; | |
| shader = gl.createShader(type); | |
| gl.shaderSource(shader, src); | |
| gl.compileShader(shader); | |
| if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { | |
| console.log(gl.getShaderInfoLog(shader)); | |
| return null; | |
| } | |
| return shader; | |
| }; | |
| linkShader = function(fragmentSrc, vertexSrc) { | |
| var shader; | |
| shader = gl.createProgram(); | |
| gl.attachShader(shader, compileShader(gl.FRAGMENT_SHADER, fragmentSrc)); | |
| gl.attachShader(shader, compileShader(gl.VERTEX_SHADER, vertexSrc)); | |
| gl.linkProgram(shader); | |
| if (!gl.getProgramParameter(shader, gl.LINK_STATUS)) { | |
| console.log("could not link shader"); | |
| return null; | |
| } | |
| return shader; | |
| }; | |
| simpleFragment = ` | |
| precision mediump float; | |
| varying vec2 vTextureCoord; | |
| varying vec4 vColor; | |
| uniform sampler2D uSampler; | |
| void main(void) { | |
| vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t)); | |
| vec4 finalColor = textureColor * vColor; | |
| gl_FragColor = finalColor; | |
| if(gl_FragColor.a < 0.01) | |
| discard; | |
| } | |
| `; | |
| simpleVertex = ` | |
| attribute vec3 aVertexPosition; | |
| attribute vec2 aTextureCoord; | |
| attribute vec4 aColor; | |
| varying vec2 vTextureCoord; | |
| varying vec4 vColor; | |
| uniform mat4 uCamera; | |
| void main(void) { | |
| gl_Position = uCamera * vec4(aVertexPosition, 1.0); | |
| vColor = aColor; | |
| vTextureCoord = aTextureCoord; | |
| } | |
| `; | |
| this.shader = linkShader(simpleFragment, simpleVertex); | |
| gl.useProgram(this.shader); | |
| this.shader.vertAttr = gl.getAttribLocation(this.shader, "aVertexPosition"); | |
| gl.enableVertexAttribArray(this.shader.vertAttr); | |
| this.shader.uvsAttr = gl.getAttribLocation(this.shader, "aTextureCoord"); | |
| gl.enableVertexAttribArray(this.shader.uvsAttr); | |
| this.shader.colorsAttr = gl.getAttribLocation(this.shader, "aColor"); | |
| gl.enableVertexAttribArray(this.shader.colorsAttr); | |
| this.shader.samplerUniform = gl.getUniformLocation(this.shader, "uSampler"); | |
| return this.shader.cameraUniform = gl.getUniformLocation(this.shader, "uCamera"); | |
| }; | |
| var initBuffers = function() { | |
| this.verts = new Float32Array(3 * 4 * this.maxSpritesInBatch); | |
| var a, i, j, ref, x, y; | |
| for (i = j = 0, ref = this.maxSpritesInBatch; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) { | |
| a = 0; | |
| x = 0; | |
| y = 0; | |
| this.verts[i * 12 + 0] = x - a; | |
| this.verts[i * 12 + 1] = y - a; | |
| this.verts[i * 12 + 2] = 0; | |
| this.verts[i * 12 + 3] = x + a; | |
| this.verts[i * 12 + 4] = y - a; | |
| this.verts[i * 12 + 5] = 0; | |
| this.verts[i * 12 + 6] = x + a; | |
| this.verts[i * 12 + 7] = y + a; | |
| this.verts[i * 12 + 8] = 0; | |
| this.verts[i * 12 + 9] = x - a; | |
| this.verts[i * 12 + 10] = y + a; | |
| this.verts[i * 12 + 11] = 0; | |
| } | |
| gl.bindBuffer(gl.ARRAY_BUFFER, this.vertsBuf); | |
| gl.bufferData(gl.ARRAY_BUFFER, this.verts, gl.DYNAMIC_DRAW); | |
| }; | |
| initShader.call(baseAtlas); | |
| initBuffers.call(baseAtlas); | |
| Atlas.prototype.startFrame = function() { | |
| let ret = istrol3d.Atlas_startFrame.call(this); | |
| gl.clearColor(0.1, 0.2, 0.3, 1.0); | |
| gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); | |
| return ret; | |
| }; | |
| Atlas.prototype.beginSprites = function(pos, zoom, viewPort, camera) { | |
| var ret = istrol3d.Atlas_beginSprites.call(this, pos, zoom, viewPort); | |
| if (!camera) { | |
| m4.scale(m4.identity(this.camera), [1 / this.viewPort[2], 1 / this.viewPort[3], 0]); | |
| return m4.mul(this.camera, m4.translate(m4.identity(m4.create), [this.viewPort[0], this.viewPort[1], 0])); | |
| } else { | |
| return this.camera = camera; | |
| } | |
| return ret; | |
| }; | |
| Atlas.prototype.drawSprite = function(src, pos, size, rot, color, z) { | |
| var _z, cos, h, i, m, mapping, sin, sx, sy, w; | |
| if (color == null) { | |
| color = [255, 255, 255, 255]; | |
| } | |
| if (z == null) { | |
| z = 0; | |
| } | |
| if (!this.ready) { | |
| return; | |
| } | |
| if (this.curSprite >= this.maxSpritesInBatch) { | |
| this.finishSprites(); | |
| } | |
| mapping = this.spriteMap[src]; | |
| if (!mapping) { | |
| console.error("not in mapping", src); | |
| return; | |
| } | |
| i = this.curSprite; | |
| m = this.drawMargin / this.originalTextureSize; | |
| this.uvs[i * 8 + 0] = mapping.uv[0] - m; | |
| this.uvs[i * 8 + 1] = mapping.uv[1] + m; | |
| this.uvs[i * 8 + 2] = mapping.uv[2] + m; | |
| this.uvs[i * 8 + 3] = mapping.uv[1] + m; | |
| this.uvs[i * 8 + 4] = mapping.uv[2] + m; | |
| this.uvs[i * 8 + 5] = mapping.uv[3] - m; | |
| this.uvs[i * 8 + 6] = mapping.uv[0] - m; | |
| this.uvs[i * 8 + 7] = mapping.uv[3] - m; | |
| this.colors[i * 16 + 0] = color[0]; | |
| this.colors[i * 16 + 1] = color[1]; | |
| this.colors[i * 16 + 2] = color[2]; | |
| this.colors[i * 16 + 3] = color[3]; | |
| this.colors[i * 16 + 4] = color[0]; | |
| this.colors[i * 16 + 5] = color[1]; | |
| this.colors[i * 16 + 6] = color[2]; | |
| this.colors[i * 16 + 7] = color[3]; | |
| this.colors[i * 16 + 8] = color[0]; | |
| this.colors[i * 16 + 9] = color[1]; | |
| this.colors[i * 16 + 10] = color[2]; | |
| this.colors[i * 16 + 11] = color[3]; | |
| this.colors[i * 16 + 12] = color[0]; | |
| this.colors[i * 16 + 13] = color[1]; | |
| this.colors[i * 16 + 14] = color[2]; | |
| this.colors[i * 16 + 15] = color[3]; | |
| w = (mapping.uv[2] - mapping.uv[0]) * this.originalTextureSize; | |
| h = (mapping.uv[1] - mapping.uv[3]) * this.originalTextureSize; | |
| sx = (w + this.drawMargin * 2) * size[0] / 2; | |
| sy = (h + this.drawMargin * 2) * size[1] / 2; | |
| cos = Math.cos(rot); | |
| sin = Math.sin(rot); | |
| if (typeof z === "function") { | |
| _z = z; | |
| } else { | |
| _z = function() { | |
| return z; | |
| }; | |
| } | |
| this.verts[i * 12 + 0] = pos[0] + (-sx * cos + sy * sin); | |
| this.verts[i * 12 + 1] = pos[1] + (-sx * sin - sy * cos); | |
| this.verts[i * 12 + 2] = _z([this.verts[i * 12 + 0], this.verts[i * 12 + 1]]); | |
| this.verts[i * 12 + 3] = pos[0] + (+sx * cos + sy * sin); | |
| this.verts[i * 12 + 4] = pos[1] + (+sx * sin - sy * cos); | |
| this.verts[i * 12 + 5] = _z([this.verts[i * 12 + 3], this.verts[i * 12 + 4]]); | |
| this.verts[i * 12 + 6] = pos[0] + (+sx * cos - sy * sin); | |
| this.verts[i * 12 + 7] = pos[1] + (+sx * sin + sy * cos); | |
| this.verts[i * 12 + 8] = _z([this.verts[i * 12 + 6], this.verts[i * 12 + 7]]); | |
| this.verts[i * 12 + 9] = pos[0] + (-sx * cos - sy * sin); | |
| this.verts[i * 12 + 10] = pos[1] + (-sx * sin + sy * cos); | |
| this.verts[i * 12 + 11] = _z([this.verts[i * 12 + 9], this.verts[i * 12 + 10]]); | |
| return this.curSprite += 1; | |
| }; | |
| Atlas.prototype.finishSprites = function(blend) { | |
| if (blend == null) { | |
| blend = false; | |
| } | |
| if (blend) { | |
| gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); | |
| } else { | |
| gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); | |
| } | |
| gl.useProgram(this.shader); | |
| gl.bindBuffer(gl.ARRAY_BUFFER, this.vertsBuf); | |
| gl.bufferData(gl.ARRAY_BUFFER, this.verts, gl.DYNAMIC_DRAW); | |
| gl.vertexAttribPointer(this.shader.vertAttr, 3, gl.FLOAT, false, 0, 0); | |
| gl.bindBuffer(gl.ARRAY_BUFFER, this.uvsBuf); | |
| gl.bufferData(gl.ARRAY_BUFFER, this.uvs, gl.DYNAMIC_DRAW); | |
| gl.vertexAttribPointer(this.shader.uvsAttr, 2, gl.FLOAT, false, 0, 0); | |
| gl.bindBuffer(gl.ARRAY_BUFFER, this.colorsBuf); | |
| gl.bufferData(gl.ARRAY_BUFFER, this.colors, gl.DYNAMIC_DRAW); | |
| gl.vertexAttribPointer(this.shader.colorsAttr, 4, gl.UNSIGNED_BYTE, true, 0, 0); | |
| gl.activeTexture(gl.TEXTURE0); | |
| gl.bindTexture(gl.TEXTURE_2D, this.texture); | |
| gl.uniform1i(this.shader.samplerUniform, 0); | |
| gl.uniformMatrix4fv(this.shader.cameraUniform, false, this.camera); | |
| gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexsBuf); | |
| gl.drawElements(gl.TRIANGLES, this.curSprite * 6, gl.UNSIGNED_SHORT, 0); | |
| return this.curSprite = 0; | |
| }; | |
| // maths.js changes | |
| m4.mul_v4 = function(m, v, d) { | |
| var w, x, y, z; | |
| if (d == null) { | |
| d = v; | |
| } | |
| x = v[0]; | |
| y = v[1]; | |
| z = v[2]; | |
| w = v[3]; | |
| d[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; | |
| d[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; | |
| d[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; | |
| d[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; | |
| return d; | |
| }; | |
| // parts.js changes | |
| parts.EnergyTransfer.prototype.draw = function() { | |
| var r; | |
| parts.EnergyTransfer.prototype.__proto__.draw.call(this); | |
| if (this.working) { | |
| r = (this.range + 40) / 255; | |
| return baseAtlas.drawSprite("img/point02.png", this.unit.pos, [r, r], 0, [255, 255, 255, 10], this.unit.z); | |
| } | |
| }; | |
| parts.StasisField.prototype.draw = function() { | |
| var a, r; | |
| parts.StasisField.prototype.__proto__.draw.call(this); | |
| if (this.working) { | |
| a = 100; | |
| } else { | |
| a = 25; | |
| } | |
| r = (this.range + 40) / 255; | |
| return baseAtlas.drawSprite("img/point02.png", [this.worldPos[0] + (Math.sin(this.unit.rot) * 100), this.worldPos[1] - (Math.cos(this.unit.rot) * 100)], [r, r], 0, [0, 0, 0, a], this.unit.z); | |
| }; | |
| // things.js changes | |
| var _color = [0,0,0,0]; | |
| Trail.prototype.draw = function() { | |
| var alive, j, len, p, ref, ref1, results, s, t; | |
| while (this.trail.length > 0 && intp.smoothStep - this.trail[0][1] > this.trailTime) { | |
| this.trail.shift(); | |
| } | |
| _color[0] = 155 + this.color[0] / 255 * 100; | |
| _color[1] = 155 + this.color[1] / 255 * 100; | |
| _color[2] = 155 + this.color[2] / 255 * 100; | |
| _color[3] = 0; | |
| s = this.trailSize; | |
| ref = this.trail; | |
| results = []; | |
| for (j = 0, len = ref.length; j < len; j++) { | |
| ref1 = ref[j], p = ref1[0], t = ref1[1]; | |
| alive = (intp.smoothStep - t) / this.trailTime; | |
| if (alive < 1 && alive > 0) { | |
| _color[3] = 255 - 255 * alive; | |
| } else { | |
| _color[3] = 0; | |
| } | |
| results.push(baseAtlas.drawSprite("img/fire02.png", p, [s, s], 0, _color, 5)); | |
| } | |
| return results; | |
| }; | |
| Particle.prototype.z = 0; | |
| Particle.prototype.draw = function() { | |
| if (this.dead) { | |
| return; | |
| } | |
| return baseAtlas.drawSprite(this.image, this.pos, this.size, this.rot, this.color, this.z - this.life); | |
| }; | |
| Bullet.prototype.draw = function() { | |
| var max; | |
| if (this.hitPos) { | |
| max = v2.distance(this.pos, this.hitPos) / this.speed; | |
| this.z = this.life * max; | |
| } else { | |
| this.z = 50; | |
| } | |
| return Bullet.prototype.__proto__.draw.call(this); | |
| }; | |
| var _offset = v2.create(); | |
| LaserBullet.prototype.draw = function() { | |
| var d, rot, w; | |
| if (this.target) { | |
| v2.set(this.target.pos, this.targetPos); | |
| } | |
| if (this.origin) { | |
| w = this.origin.weapons[this.turretNum || 0]; | |
| if (w) { | |
| v2.set(w.worldPos, this.pos); | |
| } | |
| } | |
| v2.sub(this.targetPos, this.pos, _offset); | |
| rot = v2.angle(_offset); | |
| d = v2.mag(_offset) / this.drawLength; | |
| v2.scale(_offset, .5); | |
| v2.add(_offset, this.pos); | |
| if (w) { | |
| w.rot = rot; | |
| } | |
| return baseAtlas.drawSprite(this.image, _offset, [this.size[0], d], rot, this.color, this.z + 40); | |
| }; | |
| types.Debree.prototype.draw = function() { | |
| var fade; | |
| if (this.dead) { | |
| return; | |
| } | |
| fade = this.life / this.maxLife; | |
| this.color[3] = Math.floor((1 - fade) * 255); | |
| return baseAtlas.drawSprite(this.image, this.pos, this.size, this.rot, this.color, 50 - this.life); | |
| }; | |
| // unit.js changes | |
| types.Unit.prototype.createDebree = function() { | |
| var exp, j, len, part, ref, results; | |
| ref = this.parts; | |
| results = []; | |
| for (j = 0, len = ref.length; j < len; j++) { | |
| part = ref[j]; | |
| if (Math.random() < .5 || part.decal) { | |
| continue; | |
| } | |
| exp = new types.Debree(); | |
| if (part.stripe) { | |
| exp.image = "parts/gray-" + part.image; | |
| } else { | |
| exp.image = "parts/" + part.image; | |
| } | |
| exp.z = this.z + rand() * 10 - 5; | |
| exp.pos = [0, 0]; | |
| part.computeWorldPos(); | |
| v2.set(part.worldPos, exp.pos); | |
| v2.set(this.vel, exp.vel); | |
| exp.vel[0] += (part.worldPos[0] - this.pos[0]) * .1 + rand(); | |
| exp.vel[1] += (part.worldPos[1] - this.pos[1]) * .1 + rand(); | |
| exp.rot = this.rot; | |
| exp.vrot = rand(); | |
| results.push(intp.particles[exp.id] = exp); | |
| } | |
| return results; | |
| }; | |
| types.Unit.prototype.draw = function() { | |
| var a, color, j, l, len, len1, part, partNum, r, ref, ref1, s, t, value; | |
| if (this.dead) { | |
| return; | |
| } | |
| ref = this.parts; | |
| for (j = 0, len = ref.length; j < len; j++) { | |
| part = ref[j]; | |
| if (typeof part.preDraw === "function") { | |
| part.preDraw(); | |
| } | |
| part.computeWorldPos(); | |
| } | |
| if (this.maxShield > 0) { | |
| s = this.warpIn * 2 - 1; | |
| if (s > 0) { | |
| color = [this.color[0], this.color[1], this.color[2], 255 * this.shield / this.maxShield * s]; | |
| r = (this.radius + 40) / 255; | |
| baseAtlas.drawSprite("img/point02.png", this.pos, [r, r], 0, color, this.z); | |
| } | |
| } | |
| if (this.warpIn < 1) { | |
| this.warpIn += 1 / 60; | |
| } | |
| ref1 = this.parts; | |
| for (partNum = l = 0, len1 = ref1.length; l < len1; partNum = ++l) { | |
| part = ref1[partNum]; | |
| value = part.pos[1] / 700 - .5 + this.warpIn; | |
| if (value > 0) { | |
| part.draw(); | |
| } else if (value > -0.2) { | |
| a = -Math.sin(value / Math.PI * 50); | |
| baseAtlas.drawSprite("img/unitBar/fire02.png", part.worldPos, [1, 1], 0, [255, 255, 255, 255 * a]); | |
| continue; | |
| } | |
| } | |
| if (this.energy / this.storeEnergy < .05) { | |
| t = Math.abs(Math.sin(Date.now() / 300)); | |
| baseAtlas.drawSprite("img/unitBar/energy1.png", [this.pos[0], this.pos[1] + 100 + this.radius], [1, 1], 0, [255, 100, 100, t * 200], this.z + 20); | |
| } | |
| if (control.debug && (this._pos2 != null) && this._pos) { | |
| a = 16; | |
| _offset[0] = this._pos2[0] + (this._pos[0] - this._pos2[0]) * a; | |
| _offset[1] = this._pos2[1] + (this._pos[1] - this._pos2[1]) * a; | |
| baseAtlas.drawSprite("img/pip1.png", this._pos, [1, 1], 0, [0, 255, 0, 255]); | |
| baseAtlas.drawSprite("img/pip1.png", this.pos, [1, 1], 0); | |
| return baseAtlas.drawSprite("img/pip1.png", this._pos2, [1, 1], 0, [255, 0, 0, 255]); | |
| } | |
| }; | |
| Part.prototype.draw = function() { | |
| var alpha, angle, c, flip, id, num, numParts, rot, showDamage, t; | |
| if (this.pos[0] < 0 && this.flip) { | |
| flip = -1; | |
| } else { | |
| flip = 1; | |
| } | |
| if (this.gimble) { | |
| rot = Math.PI + this.rot; | |
| } else { | |
| rot = Math.PI + this.unit.rot; | |
| } | |
| angle = Math.PI * this.dir / 2; | |
| rot += angle; | |
| if (this.canShowDamage && this.image === this.orignalImage) { | |
| numParts = this.unit.parts.length; | |
| id = this.unit.id; | |
| num = this.partNum % numParts; | |
| showDamage = num / numParts > this.unit.hp / this.unit.maxHP; | |
| if (showDamage) { | |
| t = (this.partNum + id) % 3 + 1; | |
| this.image = this.orignalImage.replace(".png", ".d" + t + ".png"); | |
| } | |
| } | |
| if (this.northWest) { | |
| if (this.dir === 0 || this.dir === 2) { | |
| this.image = this.orignalImage; | |
| } else { | |
| this.image = this.orignalImage.replace("N", "W"); | |
| } | |
| } | |
| alpha = 255; | |
| if (this.unit.cloakFade > 0) { | |
| alpha = 255 - this.unit.cloakFade * 200; | |
| } | |
| if (this.ghostCopy) { | |
| alpha = 170; | |
| } | |
| this.z = (function(_this) { | |
| return function(arg) { | |
| var x, y; | |
| x = arg[0], y = arg[1]; | |
| return (_this.unit.radius - v2.distance([x, y], _this.unit.pos)) / 3 + _this.unit.z * 50; | |
| }; | |
| })(this); | |
| if (this.stripe) { | |
| baseAtlas.drawSprite("parts/gray-" + this.image, this.worldPos, [flip, -1], rot, [255, 255, 255, alpha], (function(_this) { | |
| return function(pos) { | |
| return _this.z(pos) + 1; | |
| }; | |
| })(this)); | |
| c = this.unit.color; | |
| return baseAtlas.drawSprite("parts/red-" + this.image, this.worldPos, [flip, -1], rot, [c[0], c[1], c[2], alpha], (function(_this) { | |
| return function(pos) { | |
| return _this.z(pos) + 1; | |
| }; | |
| })(this)); | |
| } else if (this.decal) { | |
| c = this.unit.color; | |
| return baseAtlas.drawSprite("parts/" + this.image, this.worldPos, [flip / this.scale, -1 / this.scale], rot, [c[0], c[1], c[2], alpha * this.opacity], (function(_this) { | |
| return function(pos) { | |
| return _this.z(pos) + 1; | |
| }; | |
| })(this)); | |
| } else if (this.weapon) { | |
| return baseAtlas.drawSprite("parts/" + this.image, this.worldPos, [flip, -1], rot, [255, 255, 255, alpha], (function(_this) { | |
| return function() { | |
| return _this.z(_this.worldPos) + 5; | |
| }; | |
| })(this)); | |
| } else { | |
| return baseAtlas.drawSprite("parts/" + this.image, this.worldPos, [flip, -1], rot, [255, 255, 255, alpha], this.z); | |
| } | |
| }; | |
| Engine.prototype.preDraw = function() { | |
| var ref; | |
| if (v2.mag(this.unit.vel) > 1) { | |
| if (!this.trail) { | |
| this.trail = new Trail(this.unit.id, this.trailSize, this.trailTime, this.unit.color, this.unit.z * 20); | |
| } | |
| if ((ref = this.trail) != null) { | |
| ref.grow(this.worldPos); | |
| } | |
| this.trail.color = this.unit.color; | |
| return this.trail.z = this.unit.z * 20 - 3; | |
| } | |
| }; | |
| parts.JumpEngine.prototype.draw = function() { | |
| parts.JumpEngine.prototype.__proto__.draw.call(this); | |
| if (this.working) { | |
| return baseAtlas.drawSprite("parts/engineJumpPip.png", this.worldPos, [1, 1], this.unit.rot, [255, 255, 255, 255], (function(_this) { | |
| return function(pos) { | |
| return _this.z(pos) + 3; | |
| }; | |
| })(this)); | |
| } | |
| }; | |
| parts.RingTurret.prototype.draw = function() { | |
| if (this.working) { | |
| this.spin += .0010 * this.damage; | |
| this.image = "turRing.png"; | |
| } else { | |
| this.spin += .0001 * this.damage; | |
| this.image = "turRingReload.png"; | |
| } | |
| return baseAtlas.drawSprite("parts/" + this.image, this.worldPos, [1, 1], this.spin, [255, 255, 255, 255], (function(_this) { | |
| return function() { | |
| return (_this.unit.radius - v2.distance(_this.worldPos, _this.unit.pos)) / 3 + _this.unit.z * 50 + 5; | |
| }; | |
| })(this)); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment