From 42f5a514aed6b853f52932713333a75337358624 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sat, 27 May 2023 12:00:55 +0000 Subject: [PATCH] start menu --- config.js | 45 +++++++++++++++++++++------- game.js | 87 ++++++++++++++++++++++++++++++++++++++----------------- lvl1.lvl | 13 +-------- 3 files changed, 97 insertions(+), 48 deletions(-) diff --git a/config.js b/config.js index 37674ee..e505c97 100644 --- a/config.js +++ b/config.js @@ -12,7 +12,7 @@ gameobject.clone("ball", { }, start() { -// this.velocity = [-10,-300]; + this.velocity = [-10,-300]; Player.players[0].control(this); }, @@ -78,7 +78,7 @@ gameobject.clone("brick", { }, draw() { - var cl = Object.create(Rainbow.yellow); +// var cl = Object.create(Rainbow.yellow); // Debug.box(this.pos, [100,30], cl); }, @@ -114,10 +114,6 @@ var paddle = gameobject.clone("paddle", { this.frame_vel = this.frame_vel.add([1,0]); }, - draw() { - Debug.box(this.pos, [this.length, this.height], Color.white); - }, - update(dt) { this.angle = 0; this.pos = this.pos.add(this.frame_vel.scale(this.speed*dt)).map(p => Math.clamp(p, -max_x, max_x)); @@ -144,11 +140,40 @@ var paddle = gameobject.clone("paddle", { }, collide(hit) { + if (!hit.obj.from === 'ball') return; var xdiff = hit.pos.x - this.pos.x; - var obj = Game.object(hit.id); - if (Math.sign(xdiff) === Math.sign(obj.velocity)) return; - var oldvel = obj.velocity; + if (Math.sign(xdiff) === Math.sign(hit.obj.velocity)) return; + var oldvel = hit.obj.velocity; var scaler = Math.max(Math.abs(xdiff)/30, 0.9); - obj.velocity = obj.velocity.mult([-1,1]).scale(scaler); + hit.obj.velocity = hit.obj.velocity.mult([-1,1]).scale(scaler); + }, +}); + +gameobject.clone("upgrade_drop", { + fallspeed: 150, + tag: "ball", + upgrade: function() {}, + size: 30, + phys:1, + collider: circle2d.clone(), + + start() { + this.collider.radius = 15; + }, + + draw() { + Debug.box(this.pos, [this.size,this.size], Color.white); + }, + + collide(hit) { + if (!(hit.obj.from === 'paddle')) + return; + + this.upgrade(); + Register.postphys_cbs.push(() => { this.kill(); }); + }, + + update(dt) { + this.pos = this.pos.add([0,-this.fallspeed*dt]); }, }); diff --git a/game.js b/game.js index 7d00b53..745fc20 100644 --- a/game.js +++ b/game.js @@ -1,41 +1,69 @@ +var lvlwidth = 300; +var lvlheight = 500; + Debug.draw_phys(true); sim_start(); World.loadfile("lvl1.lvl"); -var b = World.spawn(gameobjects['ball']); -b.collider.sensor = true; -b.draw_layer = 4; + +function spawnball() { var bb = World.spawn(gameobjects['ball']); bb.pos = bb.pos.add([50,0]); +bb.pos.y += 200; bb.draw_layer = 3; +bb.tag = 'ball'; +} + +function spawn_powerup(pos, fn) { + var p = World.spawn(gameobjects['upgrade_drop']); + + p.pos = pos; + p.upgrade = fn; +} + +var Game = { + grow() { + + }, +}; + +spawn_powerup([0,300], () => { +var up = World.spawn(gameobjects['upgrade_drop']); +up.pos = [0,300]; + +function lostlife() { + lives--; + spawnball(); +} + +spawnball(); + +var f = World.spawn(gameobjects['edge2d']); +f.edge2d.cpoints = Geometry.box(lvlwidth,lvlheight); +f.edge2d.thickness = 10; +f.mass = 50; +//f.phys = 0; + +var killbox = World.spawn(gameobjects['polygon2d']); + +killbox.polygon2d.points = Geometry.box(lvlwidth,30); +killbox.pos = [0,-lvlheight/2]; +killbox.polygon2d.sensor = true; +killbox.register_hit(hit => { + if (hit.obj.tag === 'ball') { + Register.postphys_cbs.push(() => { + hit.obj.kill(); + lostlife(); + }); + } +}, this); //Sound.play("start.wav"); -var camcontroller = { - input_a_down() { - cmd(99); - }, - - input_d_down() { - cmd(100); - }, - - input_q_down() { - cmd(98); - }, - - input_e_down() { - cmd(97); - }, - - input_w_down() { cmd(101); }, - input_s_down() { cmd(102); }, -}; - var score = 0; var highscore = 0; var lives = 3; -function texttest() { +function gamegui() { var xpos = 1000; GUI.text("SCORE", [xpos,500],1,Color.red); @@ -52,4 +80,11 @@ function texttest() { GUI.text("[C] 2023", [xpos,230]); } -register_gui(texttest); +function startmenu() { + GUI.text("Press ENTER to start", Window.dimensions.scale(0.5)); + GUI.text("Press ESCAPE to exit", Window.dimensions.scale(0.5).add([0,-100])); +} + +register_gui(gamegui); + +register_gui(startmenu); diff --git a/lvl1.lvl b/lvl1.lvl index b7656a3..10651b3 100644 --- a/lvl1.lvl +++ b/lvl1.lvl @@ -1,15 +1,4 @@ [ - { - "from": "breakoutfield", - "edge2d": { - "degrees": 1 - }, - "pos": [ - 6.123234262925839e-17, - 0 - ], - "angle": 0 - }, { "from": "brick", "pos": [ @@ -66,4 +55,4 @@ ], "angle": 0 } -] \ No newline at end of file +]