Skip to content

Commit

Permalink
gunner component
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasballinger committed Feb 13, 2016
1 parent 89b2e20 commit fcfc4ed
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 29 deletions.
13 changes: 12 additions & 1 deletion display.ts
Expand Up @@ -98,7 +98,7 @@ var entityDraws = <{[type:string]: EntityDrawFunc}>{
}
};
var shipDraws = <{[type:string]: ShipDrawFunc}>{
'ship': function(e, ctx, dx, dy, psf, esf){
'triangle': function(e, ctx, dx, dy, psf, esf){
ctx.fillStyle="#eeaa22";
if (e.thrust > 0){
drawPoly(ctx,
Expand Down Expand Up @@ -127,6 +127,17 @@ var shipDraws = <{[type:string]: ShipDrawFunc}>{
e.h,
esf);
},
'gunship': function(e, ctx, dx, dy, psf, esf){
drawPoly(ctx,
(e.x-dx)*psf,
(e.y-dy)*psf,
[[10, 3],
[-10, 4],
[-10, -4],
[10, -3]],
e.h,
esf);
},
'boid': function(e, ctx, dx, dy, psf, esf){
ctx.fillStyle="#ffeebb";
drawPoly(ctx,
Expand Down
20 changes: 20 additions & 0 deletions entity.ts
Expand Up @@ -191,3 +191,23 @@ export class Ship extends Entity{
return new Ship(<ShipSpec>{}, undefined, undefined, undefined);
}
}

// scripts can run on these, but movement matches that of another ship
export class Component extends Ship{
constructor(spec: ShipSpec, x: number, y: number, script: Script){
super(spec, x, y, script);
}
attachedTo: Entity;
xOffset: number;
yOffset: number;
move(dt: GameTime){
if (this.attachedTo){
this.x = this.attachedTo.x + sm.x_comp(this.attachedTo.h) * this.xOffset;
this.y = this.attachedTo.y + sm.y_comp(this.attachedTo.h) * this.yOffset;
this.h = this.attachedTo.h;
}
}
deepCopyCreate():Ship{
return new Ship(<ShipSpec>{}, undefined, undefined, undefined);
} //TODO figure out the typescript way to use the correct constructor in the superclass
}
8 changes: 5 additions & 3 deletions entry.js
@@ -1,9 +1,10 @@
require("./style.css");

import { simulator } from './simulator';
import { outerspace } from './outerspace';
import { outerspace, level1 } from './outerspace';
import { Player } from './player';
import { earth } from './story';
import * as scenarios from './scenarios';

window.deepcopy = require('./deepcopy'); // needed by JS interpreter
window.Player = Player; // for convenience in onClicks of links
Expand All @@ -12,9 +13,10 @@ window.DEBUGMODE = true;

routes = {
'simulator': simulator,
'Sol': outerspace,
'Sol': function(){outerspace(scenarios.sol);},
'earth': earth,
'outfitter?': function(){'nop';}
'outfitter?': function(){'nop';},
'level1': function(){outerspace(scenarios.gunner);},
};

function getHash(){
Expand Down
3 changes: 1 addition & 2 deletions index.html
Expand Up @@ -20,8 +20,7 @@
<script>function toSpace(){ Player.fromStorage().set('location', 'Sol').go(); }</script>
<div id='spacemessage' hidden="true">
Hang out in space!<br />
Or land on a planet.<br/>
Specifically, mars.
Or land on a planet.
</div>
<div id="errorbar" hidden="true"></div>
<div id="modalbar" hidden="true"></div>
Expand Down
3 changes: 3 additions & 0 deletions interfaces.ts
Expand Up @@ -74,3 +74,6 @@ export interface Updateable {
update(e: Entity, w: SpaceWorld): void;
}

export interface Scenario {
(): WorldBuilder;
}
7 changes: 3 additions & 4 deletions outerspace.ts
Expand Up @@ -6,10 +6,10 @@ import * as scenarios from './scenarios';
import { Updater } from './updater';
import * as errorbar from './errorbar';

import { Updateable, Selection } from './interfaces';
import { Updateable, Selection, Scenario } from './interfaces';


export function outerspace(message?: string){
export function outerspace(scenario: Scenario){

// document body fullscreen
//document.body.addEventListener('click', function(e){
Expand All @@ -28,7 +28,7 @@ export function outerspace(message?: string){
function(msg){}, // queue warning
function(){ return Player.fromStorage().script; },
'canvas', // where to put key handlers
scenarios.sol(), // how to contruct a new world
scenario(), // how to contruct a new world
'JavaScript',
function(){"cleanup";},
undefined,
Expand Down Expand Up @@ -58,6 +58,5 @@ export function outerspace(message?: string){
var tickTime = updater.tick(0.032); // 30fps
setTimeout(tick, Math.max(5, 33.5-tickTime));
}
if (message){ alert(message); }
tick();
}
2 changes: 0 additions & 2 deletions player.ts
Expand Up @@ -3,7 +3,6 @@ var pilotScriptSource = require("raw!./scripts/pilot.js");
export class Player{
constructor(data: any){
for (var prop of Object.keys(data)){
console.log("setting", prop, 'to', data[prop]);
(<any>this)[prop] = data[prop];
}
}
Expand Down Expand Up @@ -32,7 +31,6 @@ export class Player{
localStorage.removeItem('player');
}
set(prop: string, value: any): Player{
console.log("setting", prop, 'to', value);
(<any>this)[prop] = value;
return this.save()
}
Expand Down
39 changes: 28 additions & 11 deletions scenarios.ts
@@ -1,15 +1,29 @@
import { SpaceWorld, makeShip, makeBoid, makePlanet } from './space';
import { SpaceWorld, makeShip, makeBoid, makePlanet, makeComponent } from './space';
import { SLgetScripts } from './scriptenv';
import { WorldBuilder } from './interfaces';
import { Player } from './player';
import * as ships from './ships';

var builtinSource = require("raw!./pilot.sl");
var builtinScripts = SLgetScripts(builtinSource);
var builtinScripts = SLgetScripts(require("raw!./scripts/pilot.sl"));

// Scenarios return a function that constructs a world when given
// a dictionary of user-provided SL scripts
interface Scenario {
(): WorldBuilder;
export var gunner = function():any{
var reset = <WorldBuilder>function reset(script: any): SpaceWorld {
var world = new SpaceWorld();
var p = Player.fromStorage().spaceLocation;
var ship = makeComponent(ships.Gunship, p[0], p[1], 270, script);
ship.imtheplayer = true;
world.addBackgroundEntity(makePlanet(400, 300, 40, '#ab43af'));
world.addEntity(makeShip(ships.Triangle, -300, 350, 270, builtinScripts.citizenScript));
world.addEntity(makeShip(ships.Triangle, -500, 250, 170, builtinScripts.citizenScript));
world.addEntity(makeShip(ships.Triangle, 200, 250, 90, builtinScripts.citizenScript));
world.addEntity(ship); // adding the ship last means it goes in front
(<any>window).world = world;
return world;
}
reset.instructions = "i"
return reset
}

export var scenario1 = function():any{
Expand All @@ -32,13 +46,13 @@ export var scenario1 = function():any{

var world = new SpaceWorld();

var ship = makeShip(-200, 350, 270, playerScript);
var ship = makeShip(ships.Triangle, -200, 350, 270, playerScript);
(<any>window).ship = ship;
var ship2 = makeShip(-300, 350, 270, enemyScript);
var ship2 = makeShip(ships.Triangle, -300, 350, 270, enemyScript);
ship.imtheplayer = true;
world.addEntity(ship);
world.addEntity(ship2);
//world.addEntity(makeShip(70, 190, 270, scripts.pilotScript));
//world.addEntity(makeShip(ships.Triangle, 70, 190, 270, scripts.pilotScript));
for (var i=0; i<boidArgs.length; i++){
world.addEntity(makeBoid(boidArgs[i][0], boidArgs[i][1], boidArgs[i][2],
boidArgs[i][3], boidArgs[i][4], boidArgs[i][5],
Expand All @@ -56,14 +70,17 @@ export var sol = function():any{
var reset = <WorldBuilder>function reset(script: any): SpaceWorld {
var world = new SpaceWorld();
var p = Player.fromStorage().spaceLocation;
var ship = makeShip(p[0], p[1], 270, script);
var ship = makeShip(ships.Triangle, p[0], p[1], 270, script);
ship.imtheplayer = true;
var earth = makePlanet(100, 100, 50);
earth.onLand = function(){
console.log('landed on earth');
Player.fromStorage().set('location', 'earth').go();
}
var luna = makePlanet(300, 200, 30, '#eeeebb');
luna.onLand = function(){
Player.fromStorage().set('location', 'level1').go();
}
var mars = makePlanet(-200, 1300, 45, '#ee3333');
mars.onLand = function(){
console.log('landed on mars');
Expand All @@ -72,8 +89,8 @@ export var sol = function():any{
world.addBackgroundEntity(earth);
world.addBackgroundEntity(luna);
world.addBackgroundEntity(mars);
world.addEntity(makeShip(-300, 350, 270, builtinScripts.enemyScript));
world.addEntity(makeShip(-500, 250, 270, builtinScripts.enemyScript));
world.addEntity(makeShip(ships.Triangle, -300, 350, 270, builtinScripts.enemyScript));
world.addEntity(makeShip(ships.Triangle, -500, 250, 270, builtinScripts.enemyScript));
world.addEntity(ship); // adding the ship last means it goes in front
(<any>window).world = world;
return world;
Expand Down
2 changes: 2 additions & 0 deletions scriptenv.ts
Expand Up @@ -30,6 +30,8 @@ var funcs = {
}
return a === b;
},
'and': function(a: boolean, b: boolean){ return a && b; },
'or': function(a: boolean, b: boolean){ return a || b; },
}
// Even though it wouldn't hurt to copy this object, all the functions would
// be deepCopy passthroughs anyway. If a way to *modify* this array were added,
Expand Down
12 changes: 11 additions & 1 deletion pilot.sl → scripts/pilot.sl
Expand Up @@ -32,7 +32,7 @@
(leftFor .1)))
; (forever
; (if (and (> y 400) (> e.dy 0))
; (do (turnTo 270)
; (do (turnTo 270)
; (thrustFor .2))
; (if (and (< y 200) (< dy 0))
; (do (turnTo 90)
Expand All @@ -41,6 +41,16 @@
; (ping spot)
; (fireMissile)))

(defn citizenScript ()
(forever
(thrustFor .1)
(if (and (> y 400) (> dy 0))
(do (turnTo 270)
(thrustFor .2))
(if (and (< y 400) (< dy 0))
(do (turnTo 90)
(thrustFor .2))))))

; when syntax checking: x, y, dx, dy, and others
; are all dynamic scope. Assigning to them is an error.
;
Expand Down
17 changes: 15 additions & 2 deletions ships.ts
Expand Up @@ -10,8 +10,8 @@ export var Boid = {
isInertialess: false,
lifespan: <number>undefined,
}
export var Ship = {
type: 'ship',
export var Triangle = {
type: 'triangle',
r: 10,
maxThrust: 300,
maxDH: 200,
Expand All @@ -23,6 +23,19 @@ export var Ship = {
lifespan: <number>undefined,
}

export var Gunship = {
type: 'gunship',
r: 3,
maxThrust: 0,
maxDH: 0,
maxSpeed: 0,
isMunition: false,
explosionSize: 12,
armorMax: 20,
isInertialess: false,
lifespan: <number>undefined,
}

export var DroneMissile = {
type: 'dronemissile',
r: 10,
Expand Down
12 changes: 9 additions & 3 deletions space.ts
@@ -1,4 +1,4 @@
import { Entity, Ship, Spob } from './entity';
import { Entity, Ship, Spob, Component } from './entity';
import { x_comp, y_comp, dist } from './shipmath';
import * as ships from './ships';
import * as scriptEnv from './scriptenv';
Expand All @@ -18,12 +18,18 @@ export function makeBoid(x:number, y:number, dx:number, dy:number, h:number, dh:
return boid;
}

export function makeShip(x: number, y: number, h: number, script: Script){
var ship = new Ship(ships.Ship, x, y, script);
export function makeShip(kind: ShipSpec, x: number, y: number, h: number, script: Script){
var ship = new Ship(kind, x, y, script);
ship.h = h;
return ship;
}

export function makeComponent(kind: ShipSpec, x: number, y: number, h: number, script: Script): Ship{
var ship = new Component(kind, x, y, script);
ship.h = h;
return <Ship>ship;
}

function makeMissile(x: number, y: number, dx: number, dy: number, h: number, ship: ShipSpec, script: Script){
var missile = new Ship(ship, x, y, script);
missile.dx = dx;
Expand Down

0 comments on commit fcfc4ed

Please sign in to comment.