breakout works

This commit is contained in:
John Alanbrook 2023-09-08 17:34:48 +00:00
parent 22fb3de804
commit bd7080d163
7 changed files with 49 additions and 34 deletions

View file

@ -1,12 +1,14 @@
Object.assign(self, { Object.assign(self, {
collider: circle2d.clone(), collider: circle2d.clone({radius:5}),
img: sprite.clone({path:"ball.png"}), img: sprite.clone({path:"ball.png"}),
phys: 0, phys: 0,
elasticity: 1, elasticity: 1,
friction: 0, friction: 0,
scale: 10, tag: 'ball',
collide(hit) { collide(hit) {
Sound.play("bump.wav"); Sound.play("bump.wav");
}, },
}); });
self.img.pos = sprite.POS_MID;

View file

@ -3,7 +3,8 @@ var score = 0;
var highscore = 0; var highscore = 0;
var lives = 3; var lives = 3;
var brick = World.spawn(prototypes.get_ur('brick')); /* Spawn & kill a brick to get its width and height */
var brick = prototypes.get_ur('brick').make(World);
var brickbb = brick.char.boundingbox; var brickbb = brick.char.boundingbox;
var bwidth = brickbb.r - brickbb.l; var bwidth = brickbb.r - brickbb.l;
var bheight = brickbb.t-brickbb.b; var bheight = brickbb.t-brickbb.b;
@ -15,13 +16,16 @@ var cols = 13;
var lvlwidth = bwidth*cols; var lvlwidth = bwidth*cols;
var lvlheight = 600; var lvlheight = 600;
//var frameworld = World var f = prototypes.get_ur('edge2d').make(World);
var f = World.spawn(prototypes.get_ur('edge2d'));
f.edge2d.thickness = 10; f.edge2d.thickness = 10;
f.edge2d.cpoints = Geometry.box(lvlwidth+f.edge2d.thickness*2,lvlheight); f.edge2d.cpoints = Geometry.box(lvlwidth+f.edge2d.thickness*2,lvlheight-30);
f.edge2d.degrees = 1;
f.scale = 1;
f.edge2d.sync();
var drawColor = Color.green.slice();
drawColor.a = 100;
f.draw = function() { f.draw = function() {
//Debug.line(f.edge2d.points, Color.green, 0, f.edge2d.thickness*2); Debug.line(f.edge2d.points, drawColor, 0, f.edge2d.thickness*2);
}; };
Register.draw.register(f.draw,f); Register.draw.register(f.draw,f);
@ -83,7 +87,7 @@ for (var row = 0; row < rows; row++) {
if (lvl.bricks[row][col] === 0) if (lvl.bricks[row][col] === 0)
continue; continue;
var brick = World.spawn(prototypes.get_ur('brick')); var brick = prototypes.get_ur('brick').make(World);
brick.powerup = function() { brick.powerup = function() {
if (Math.random() < 0.1) if (Math.random() < 0.1)
spawn_random_powerup(this.pos); spawn_random_powerup(this.pos);
@ -110,19 +114,19 @@ var flashtimer = timer.make(function() {
flashtimer.time = Math.random_range(1,3); flashtimer.time = Math.random_range(1,3);
}, Math.random_range(1,3)); }, Math.random_range(1,3));
var paddle = World.spawn(prototypes.get_ur('paddle')); var paddle = prototypes.get_ur('paddle').make(self);
paddle.extents = lvlwidth/2 - f.edge2d.thickness; paddle.extents = lvlwidth/2 - f.edge2d.thickness;
paddle.pos = [0,-lvlheight/2+paddle.height*4]; paddle.pos = [0,-lvlheight/2+50];
paddle.setgrow(3); paddle.setgrow(3);
paddle.lasership();
Player.players[0].control(paddle); Player.players[0].control(paddle);
paddle.inputs.space();
Gamestate.spawnball = function() { Gamestate.spawnball = function() {
var bb = World.spawn(prototypes.get_ur('ball')); var bb = prototypes.get_ur('ball').make(self);
bb.pos = bb.pos.add([0,-200]); bb.pos = bb.pos.add([0,-200]);
bb.velocity = [30,200]; bb.velocity = [30,200];
bb.draw_layer = 3; bb.draw_layer = 3;
bb.tag = 'ball';
} }
var powerups = {}; var powerups = {};
@ -208,7 +212,7 @@ var last_powerup = {};
function spawn_random_powerup(pos) function spawn_random_powerup(pos)
{ {
if (prototypes.get_ur('upgrade_drop').instances.length !== 0) return; if (prototypes.get_ur('upgrade_drop').instances.length !== 0) return;
var p = World.spawn(prototypes.get_ur('upgrade_drop')); var p = prototypes.get_ur('upgrade_drop').make(self);
p.pos = pos; p.pos = pos;
var power = select_weighted(powerups.array()); var power = select_weighted(powerups.array());
@ -222,7 +226,7 @@ function spawn_random_powerup(pos)
} }
function spawn_powerup(pos, fn) { function spawn_powerup(pos, fn) {
var p = World.spawn(prototypes.get_ur('upgrade_drop')); var p = prototypes.get_ur('upgrade_drop').make(self);
p.pos = pos; p.pos = pos;
p.upgrade = fn; p.upgrade = fn;
return p; return p;
@ -255,7 +259,7 @@ function lostlife() {
}; };
Gamestate.spawnball(); Gamestate.spawnball();
var killbox = World.spawn(prototypes.get_ur('polygon2d')); var killbox = prototypes.get_ur('polygon2d').make(self);
killbox.polygon2d.points = Geometry.box(lvlwidth,30); killbox.polygon2d.points = Geometry.box(lvlwidth,30);
killbox.pos = [0,-lvlheight/2]; killbox.pos = [0,-lvlheight/2];
@ -268,6 +272,8 @@ killbox.register_hit(hit => {
}, killbox); }, killbox);
killbox.polygon2d.sync();
//Sound.play("start.wav"); //Sound.play("start.wav");
var pause = { var pause = {

View file

@ -1,5 +1,4 @@
Object.assign(self, { Object.assign(self, {
collider: polygon2d.clone(),
char: char2d.clone({ char: char2d.clone({
flash:Resources.load("brick.png"), flash:Resources.load("brick.png"),
}), }),
@ -21,10 +20,16 @@ Object.assign(self, {
}, },
collide(hit) { collide(hit) {
if (hit.obj.from !== 'ball') if (hit.obj.tag !== 'ball')
return; return;
this.powerup?.(); this.powerup?.();
this.kill(); this.kill();
}, },
}); });
self.collider = polygon2d.clone();
self.collider.points = [[-8,4]];
self.collider.flipx = true;
self.collider.flipy = true;
self.char.pos = [-0.5, -0.5];

View file

@ -1,2 +1,5 @@
Window.icon("powerup.png"); Window.icon("powerup.png");
World.load("startmenu"); gameobject.scale = 2;
//World.load(prototypes.get_ur("startmenu"));
World.run("startmenu.js");

View file

@ -3,6 +3,7 @@ Object.assign(self, {
img: sprite.clone(), img: sprite.clone(),
phys: 1, phys: 1,
elasticity: 1, elasticity: 1,
tag: 'paddle',
speed: 1000, speed: 1000,
@ -23,9 +24,6 @@ Object.assign(self, {
fpos.x = Math.clamp(fpos.x,-this.max_x,this.max_x); fpos.x = Math.clamp(fpos.x,-this.max_x,this.max_x);
this.pos = fpos; this.pos = fpos;
this.frame_vel = [0,0]; this.frame_vel = [0,0];
if (this.stickball)
this.stickball.pos = this.pos.add(this.stickpos);
}, },
physupdate(dt) { this.velocity = [0,0]; }, physupdate(dt) { this.velocity = [0,0]; },
@ -66,23 +64,21 @@ Object.assign(self, {
}, },
shoot() { shoot() {
var m1 = World.spawn(gameobjects['missile']); var m1 = World.spawn(prototypes.get_ur("missile"));
var m2 = World.spawn(gameobjects['missile']); var m2 = m1.dup();
m1.pos = this.pos.add([(this.length/2)-5,0]); m1.pos = this.pos.add([(this.length/2)-5,0]);
m2.pos = this.pos.add([5-(this.length/2),0]); m2.pos = this.pos.add([5-(this.length/2),0]);
}, },
sticky: false, sticky: false,
stored_vel: [0,0], stored_vel: [0,0],
stickball: {},
stickpos: [0,0],
start() { start() {
this.collider.sensor = true; this.collider.sensor = true;
}, },
collide(hit) { collide(hit) {
if (!(hit.obj.from === 'ball')) return; if (!(hit.obj.tag === 'ball')) return;
var xdiff = hit.pos.x - this.pos.x; var xdiff = hit.pos.x - this.pos.x;
var hitangle = (xdiff/this.length + 0.5) * Math.PI; var hitangle = (xdiff/this.length + 0.5) * Math.PI;
@ -97,18 +93,18 @@ Object.assign(self, {
if (this.sticky) { if (this.sticky) {
this.stored_vel = hit.obj.velocity; this.stored_vel = hit.obj.velocity;
hit.obj.velocity = [0,0]; hit.obj.velocity = [0,0];
hit.obj.reparent(this);
this.stickball = hit.obj; this.stickball = hit.obj;
this.stickpos = hit.obj.pos.sub(this.pos);
} }
}, },
}); });
self.inputs = {}; self.inputs = {};
self.inputs.space = function() { self.inputs.space = function() {
Log.warn("PADDLE SPACE");
if (this.sticky && this.stickball) { if (this.sticky && this.stickball) {
this.stickball.velocity = this.stored_vel; this.stickball.velocity = this.stored_vel;
this.stickball = {}; this.stickball.reparent(World);
delete this.stickball;
} }
if (this.laser) { if (this.laser) {
@ -125,3 +121,5 @@ self.inputs.d = {};
self.inputs.d.down = function() { self.inputs.d.down = function() {
this.frame_vel = this.frame_vel.add([1,0]); this.frame_vel = this.frame_vel.add([1,0]);
}; };
self.img.pos = sprite.POS_MID;

View file

@ -26,11 +26,11 @@
"ball": { "ball": {
"mass": 0.00001, "mass": 0.00001,
"collider": { "collider": {
"radius": 4.39338754070273,
"ofset": [ "ofset": [
0, 0,
0 0
] ],
"radius": 2
}, },
"img": { "img": {
"pos": [ "pos": [

View file

@ -6,6 +6,7 @@ Object.assign(self, {
phys:1, phys:1,
collider: circle2d.clone(), collider: circle2d.clone(),
color: Color.Rainbow.blue, color: Color.Rainbow.blue,
tag: 'powerup',
start() { start() {
this.collider.radius = this.size; this.collider.radius = this.size;
@ -17,7 +18,7 @@ Object.assign(self, {
}, },
collide(hit) { collide(hit) {
if (!(hit.obj.from === 'paddle')) if (!(hit.obj.tag === 'paddle'))
return; return;
this.upgrade(); this.upgrade();