// ship.js // Player's ship and a blaster. var SHIP_SPEED = 0.00004; var SIGHT_DISTANCE = 0.0008; var sicon = new GIcon; sicon.image = "images/ship.png"; sicon.shadow = "images/dummy_shadow.png"; sicon.iconSize = new GSize(16, 16); sicon.shadowSize = new GSize(1, 1); sicon.iconAnchor = new GPoint(8, 8); var sgicon = new GIcon(sicon); sgicon.image = "images/sight.png"; var imicon = new GIcon(sicon); imicon.image = "images/impact.png"; var blicon = new GIcon(sicon); blicon.image = "images/blaster.png"; function Ship() { this.marker = new GMarker( new GPoint(mapManager.centerX, (mapManager.bounds.maxY - mapManager.bounds.minY) / 4 + mapManager.bounds.minY), sicon); this.sightMarker = new GMarker( new GPoint(mapManager.centerX, mapManager.centerY), sgicon); this.impactMarker = new GMarker( new GPoint(mapManager.centerX, mapManager.centerY), imicon); this.blasterMarker = new GMarker( new GPoint(mapManager.centerX, mapManager.centerY), blicon); this.blasterCnt = 0; map.addOverlay(this.marker); map.addOverlay(this.sightMarker); this.blasterExplosion = new Explosion(); this.shipExplosion = new Explosion(); this.update = function() { if (key[KEY_LEFT]) this.marker.point.x -= SHIP_SPEED; if (key[KEY_RIGHT]) this.marker.point.x += SHIP_SPEED; if (key[KEY_UP]) this.marker.point.y += SHIP_SPEED; if (key[KEY_DOWN]) this.marker.point.y -= SHIP_SPEED; this.marker.point.x += mapManager.velX; this.marker.point.y += mapManager.velY; if (this.marker.point.x < mapManager.bounds.minX) this.marker.point.x = mapManager.bounds.minX; if (this.marker.point.x > mapManager.bounds.maxX) this.marker.point.x = mapManager.bounds.maxX; if (this.marker.point.y < mapManager.bounds.minY) this.marker.point.y = mapManager.bounds.minY; if (this.marker.point.y > mapManager.bounds.maxY - SIGHT_DISTANCE) this.marker.point.y = mapManager.bounds.maxY - SIGHT_DISTANCE; var xr = (this.marker.point.x - mapManager.bounds.minX) / (mapManager.bounds.maxX - mapManager.bounds.minX); var yr = (this.marker.point.y - mapManager.bounds.minY) / (mapManager.bounds.maxY - mapManager.bounds.minY); var mvx = 0; if (xr < 0.3) mvx = -(0.3 - xr) * 7 * MAP_SCROLL_SPEED; else if (xr > 0.7) mvx = (xr - 0.7) * 7 * MAP_SCROLL_SPEED; mapManager.velX += (mvx - mapManager.velX) * 0.1; var mvy = MAP_SCROLL_SPEED; mvy = (1 + yr * 5) * MAP_SCROLL_SPEED; mapManager.velY += (mvy - mapManager.velY) * 0.1; this.sightMarker.point.x = this.marker.point.x; this.sightMarker.point.y = this.marker.point.y + SIGHT_DISTANCE; this.marker.redraw(true); this.sightMarker.redraw(true); if (this.blasterCnt <= 0 && key[KEY_SHOT]) { this.blasterCnt = 15; this.impactMarker.point.x = this.sightMarker.point.x; this.impactMarker.point.y = this.sightMarker.point.y; this.blasterMarker.point.x = this.marker.point.x; this.blasterMarker.point.y = this.marker.point.y; map.addOverlay(this.impactMarker); map.addOverlay(this.blasterMarker); } if (this.blasterCnt > 0) { this.blasterMarker.point.y += 0.000055; this.blasterMarker.redraw(true); this.blasterCnt--; if (this.blasterCnt <= 0) { map.removeOverlay(this.impactMarker); map.removeOverlay(this.blasterMarker); this.blasterExplosion.set(this.impactMarker.point, true); checkTurretsHit(this.impactMarker.point); } } this.blasterExplosion.update(); this.shipExplosion.update(); } this.checkHit = function(p) { if (Math.abs(p.x - this.marker.point.x) < 0.00005 && Math.abs(p.y - this.marker.point.y) < 0.00005) this.destroyed(); } this.destroyed = function() { this.shipExplosion.set(this.marker.point, false); } } // Handling a key input. var keyMap = new Array(256); var key = [false, false, false, false, false]; var KEY_UP = 0; var KEY_LEFT = 1; var KEY_DOWN = 2; var KEY_RIGHT = 3; var KEY_SHOT = 4; keyMap[38] = KEY_UP + 1; keyMap[37] = KEY_LEFT + 1; keyMap[40] = KEY_DOWN + 1; keyMap[39] = KEY_RIGHT + 1; keyMap[90] = KEY_SHOT + 1; document.onkeydown = function(e) { var kc = (window.event) ? event.keyCode : e.keyCode; setKey(kc, true); } document.onkeyup = function(e) { var kc = (window.event) ? event.keyCode : e.keyCode; setKey(kc, false); } setKey = function(kc, v) { if (kc < 256) { km = keyMap[kc]; if (km > 0) key[km - 1] = v; } }