breakout/breakout.js
2023-06-05 15:31:57 +00:00

207 lines
4.1 KiB
JavaScript

var score = 0;
var highscore = 0;
var lives = 3;
var balls = 0;
var lvlwidth = 450;
var lvlheight = 600;
let bricks = 0;
let allbricks = [];
//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 wingame = function() {
allballs.forEach(x=>x.kill());
Log.warn("WINNER");
Player.players[0].uncontrol(paddle);
register_gui(function() {
GUI.text("WINNER", Window.dimensions.scale(0.5));
});
}
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']);
allbricks.push(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);
let allballs = [];
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++;
allballs.push(bb);
}
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') {
hit.obj.kill();
lostball();
}
}, killbox);
//Sound.play("start.wav");
let gamepawn =
{
input_escape_pressed() {
backtomain();
},
input_o_pressed() {
/* Get all bricks and kill */
allbricks.forEach(x => x.kill());
},
}
function gamegui() {
GUI.column({
items: [
GUI.text_fn("SCORE", {color: Color.red}),
GUI.text_fn(`${score}`),
GUI.text_fn("HIGH SCORE", {color: Color.red}),
GUI.text_fn(`${highscore}`),
GUI.text_fn(`LIVES: ${lives}`),
GUI.text_fn("YARKANOID", {color: [5,120,240,255]}),
GUI.text_fn("ODPLOT GAMES"),
GUI.text_fn("[C] 2023"),
]
}).draw(Window.dimensions.scale([0.9,0.8]));
}
register_gui(gamegui);
Player.players[0].control(gamepawn);
let backtomain = function()
{
World.clear();
run("startmenu.js");
unregister_gui(gamegui);
Player.players[0].uncontrol(gamepawn);
}