Created
April 20, 2019 00:07
-
-
Save jonbro/1696afbd5457ef96f7993b2187c31cf2 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* todo | |
* make the powerups actually do something | |
* enemy count | |
* sub bullets (spawn a child bullet at age) | |
* enemy fire rate | |
* bullet count | |
* enemy movement | |
* homing enemy bullets | |
* player bullet spread | |
* player bullet count | |
* player orientation | |
* kill bullets if they exit the screen | |
* sound | |
* make the enemy segmented | |
* particles | |
* write a range function (leftBound, rightBound, count, index) | |
* returns for (0, 1, 1, 0) = 0.5 | |
* returns for (0, 1, 2, 0) = 0 | |
* */ | |
import * as m from "mglue"; | |
import { Vector, Color, Keyboard, Random, TextDrawer, Sound } from "mglue"; | |
class BasicGame extends m.Game | |
{ | |
level : number = 0; | |
ship : Ship; | |
onBeginGame() | |
{ | |
this.ship = new Ship(); | |
this.nextLevel(); | |
} | |
nextLevel() | |
{ | |
this.level++; | |
new Pickup(0.25) | |
new Pickup(0.75) | |
} | |
update() | |
{ | |
} | |
} | |
class Enemy extends m.Actor | |
{ | |
hp : number = 2; | |
begin() | |
{ | |
this.hp = g.level + 2; | |
this.position.set(0.5, 0.1) | |
this.drawing | |
.setColor(Color.pink) | |
.addRect(0.05) | |
} | |
update() | |
{ | |
if(this.age%30==0) | |
{ | |
let direction = new m.Vector(0, 1); | |
// aim routine | |
direction = g.ship.position.copy().subtract(this.position).normalize(); | |
new EnemyShot(this.position.copy(),direction); | |
} | |
} | |
hit() | |
{ | |
this.hp--; | |
if(this.hp == 0) | |
{ | |
this.destroy(); | |
g.nextLevel(); | |
} | |
} | |
} | |
class Pickup extends m.Actor | |
{ | |
text : m.TextActor; | |
begin(px) | |
{ | |
this.text = new m.TextActor("POW"); | |
this.text.setDurationForever(); | |
this.text.setVelocity(new m.Vector(0, 0.002)); | |
this.text.position.set(px, 0) | |
this.drawing | |
.setColor(m.Color.orange) | |
.addRect(0.1, 0.05); | |
} | |
update() | |
{ | |
this.text.update(); | |
this.position = this.text.position.copy(); | |
if(this.text.position.y > g.ship.position.y) | |
{ | |
this.text.velocity.y = 0; | |
this.text.position.y = g.ship.position.y; | |
} | |
this.checkOverlap(Ship, (s:Ship)=>{ | |
this.onGrab(); | |
})); | |
} | |
onGrab() | |
{ | |
this.group.members.forEach((p : Actor)=>{ | |
p.destroy(); | |
p.text.destroy(); | |
}); | |
new Enemy(); | |
} | |
} | |
class EnemyShot extends m.Actor | |
{ | |
begin(pos, direction) | |
{ | |
this.position = pos; | |
this.drawing | |
.setColor(Color.pink) | |
.addRect(0.01) | |
direction.multiply(0.01); | |
this.velocity = direction; | |
} | |
} | |
let SPEED_MULT = 1; | |
class Ship extends m.Actor | |
{ | |
acceleration : Vector = new Vector(0,0); | |
speed : number = 0.0017 * SPEED_MULT; | |
maxVelocity : number = 0.019 * SPEED_MULT; | |
begin() | |
{ | |
this.position.set(0.5,0.9) | |
this.drawing | |
.setColor(Color.blue) | |
.addSegementedRect(0.01, 0.05,-0.005,0,-20) | |
.addSegementedRect(0.01, 0.05, 0.005,0,20) | |
.mirrorX() | |
} | |
update() | |
{ | |
this.acceleration.set(0,0); | |
if(Keyboard.keyDown[Keyboard.LEFT]) | |
{ | |
this.acceleration.set(-this.speed, 0); | |
} | |
else if(Keyboard.keyDown[Keyboard.RIGHT]) | |
{ | |
this.acceleration.set(this.speed, 0); | |
} | |
else | |
{ | |
this.velocity.multiply(0.8); | |
} | |
this.velocity.add(this.acceleration); | |
this.velocity.x = this.velocity.x.clamp(-this.maxVelocity, this.maxVelocity); | |
let clampedX = this.position.x.clamp(0, 1); | |
// zero velocity when we hit the walls | |
if(clampedX != this.position.x) | |
{ | |
this.velocity.x = 0; | |
this.position.x = clampedX; | |
} | |
this.checkOverlap(EnemyShot, (e : EnemyShot) =>{ | |
this.kill() | |
}); | |
if(this.age%20==0) | |
{ | |
new PlayerShot(this.position.copy()); | |
} | |
} | |
kill() | |
{ | |
this.destroy() | |
g.endGame(); | |
} | |
} | |
class PickupCore | |
{ | |
onPickup() | |
{} | |
enemyUpdate(e:Enemy) | |
{} | |
} | |
class PlayerShot extends m.Actor | |
{ | |
begin(pos) | |
{ | |
this.position = pos; | |
this.drawing | |
.setColor(Color.blue) | |
.addRect(0.02) | |
this.velocity.set(0, -0.03) | |
} | |
update() | |
{ | |
this.checkOverlap(Enemy, (e : Enemy)=>{ | |
e.hit() | |
this.destroy(); | |
}); | |
} | |
} | |
var g: BasicGame; | |
m.Config.title = "NEW GAME"; | |
m.Config.saveName = "newGame"; | |
m.Game.runOnReady(() => | |
{ | |
g = new BasicGame(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment