breakout/breakout.js

201 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-05-29 10:47:47 -05:00
var score = 0;
var highscore = 0;
var lives = 3;
var balls = 0;
var lvlwidth = 450;
var lvlheight = 600;
let bricks = 0;
//var frameworld = World
var f = World.spawn(gameobjects['edge2d']);
f.edge2d.cpoints = Geometry.box(lvlwidth,lvlheight);
f.edge2d.thickness = 10;
f.draw = function() {
Debug.line(f.edge2d.points, Color.green, 0, f.edge2d.thickness*2);
};
register_draw(f.draw,f);
Debug.draw_phys(true);
var brick = World.spawn(gameobjects['brick']);
var brickbb = brick.char.boundingbox;
var bwidth = brickbb.r - brickbb.l;
var bheight = brickbb.t-brickbb.b;
brick.kill();
var fitbricks = Math.floor(lvlwidth/bwidth);
let towin = function()
{
}
let wingame = function() {
towin();
}
let killbrick = function() {
bricks--;
if (bricks <= 0)
wingame();
};
var rowbricks = 8;
for (var row = 0; row < rowbricks; row++) {
for (var col = 0; col < 6; col++) {
var brick = World.spawn(gameobjects['brick']);
brick.stop = function() { killbrick(); };
bricks++;
var bcolor = Rainbow.red;
switch (col) {
case 0:
bcolor = Rainbow.red;
break;
case 1:
bcolor = Rainbow.orange;
break;
case 2:
bcolor = Rainbow.darkorange;
break;
case 3:
bcolor = Rainbow.yellow;
break;
case 4:
bcolor = Rainbow.green;
break;
case 5:
bcolor = Rainbow.blue;
break;
}
brick.char.color = bcolor;
var startcol = lvlheight/2-50; // 50 pixel gap between top and brick start
let x = -(rowbricks/2) * bwidth;
var pos = [x+bwidth*row+bwidth/2, startcol-bheight*col];
brick.pos = pos;
if (Math.random() < 0.1) {
if (Math.random() < 0.5) {
brick.powerup = function() {
var p = spawn_powerup(this.pos, () => Gamestate.spawnball());
p.color = Rainbow.blue;
}
} else {
brick.powerup = function() {
var p = spawn_powerup(this.pos, () => paddle.grow());
p.color = Rainbow.green;
}
}
}
}
}
var paddle = World.spawn(gameobjects['paddle']);
paddle.extents = lvlwidth/2 - f.edge2d.thickness;
paddle.pos = [0,-lvlheight/2+paddle.height*4];
paddle.setgrow(1);
Gamestate.spawnball = function() {
var bb = World.spawn(gameobjects['ball']);
bb.pos = bb.pos.add([0,-200]);
bb.velocity = [30,300];
bb.draw_layer = 3;
bb.tag = 'ball';
balls++;
}
function spawn_powerup(pos, fn) {
var p = World.spawn(gameobjects['upgrade_drop']);
p.pos = pos;
p.upgrade = fn;
return p;
}
function multiball(n) {
for (var i = 0; i < n; i++)
Gamestate.spawnball();
};
function lostball()
{
balls--;
if (balls === 0)
lostlife();
};
function lostgame()
{
Log.warn("LOSER");
backtomain();
}
function lostlife() {
lives--;
if (lives === 0)
lostgame();
else
Gamestate.spawnball();
};
Gamestate.spawnball();
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')
return;
hit.obj.kill();
lostball();
}, killbox);
//Sound.play("start.wav");
let gamepawn =
{
input_escape_pressed() {
backtomain();
},
input_o_pressed() {
/* Get all bricks and kill */
},
}
function gamegui() {
var xpos = 1000;
GUI.text("SCORE", [xpos,500],1,Color.red);
GUI.text(`${score}`, [xpos,480],1);
GUI.text("HIGH SCORE", [xpos,450],1, Color.red);
GUI.text(`${highscore}`, [xpos,430],1);
GUI.text(`LIVES: ${lives}`, [xpos, 400], 1, Color.white);
GUI.text("YARKANOID", [xpos,300], 1,[5,120,240]);
GUI.text("ODPLOT GAMES", [xpos,250],1);
GUI.text("[C] 2023", [xpos,230]);
}
register_gui(gamegui);
Player.players[0].control(gamepawn);
let backtomain = function()
{
World.clear();
run("startmenu.js");
unregister_gui(gamegui);
Player.players[0].uncontrol(gamepawn);
}