breakout/config.js
2023-06-07 17:42:59 +00:00

204 lines
3.8 KiB
JavaScript

gameobject.elasticity = 1;
gameobject.clone("ball", {
collider: circle2d.clone(),
img: sprite.clone(),
phys: 0,
elasticity: 1,
friction: 0,
collide(hit) {
Sound.play("bump.wav");
},
});
Color.Gameboy = {
darkest: [15,56,15],
dark: [48,98,48],
light: [139,172,15],
lightest: [155,188,15]
};
Color.Rainbow = {
red: [204,0,1],
orange: [255,102,0],
darkorange: [205,102,1],
yellow: [205,204,0],
green: [0,153,1],
blue: [53,51,255]
};
gameobject.clone("brick", {
collider: polygon2d.clone(),
char: char2d.clone({
flash:Resources.load("brick.png"),
}),
points: 50,
start() {
this.char.stop();
this.flash();
this.char.color = Color.Rainbow.red;
},
flash() {
this.char.play("flash");
},
collide(hit) {
if (hit.obj.from !== 'ball')
return;
this.powerup?.();
this.kill();
},
});
var act_x = Action.add_new("move");
act_x.inputs.push("axis_ljoy");
var paddle = gameobject.clone("paddle", {
collider: polygon2d.clone(),
img: sprite.clone(),
phys: 1,
elasticity: 1,
speed: 1000,
length: 50,
height: 28,
lengths: [16,24,32,40,48],
growlvl: 1,
maxgrow: 5,
max_x: 0,
extents: 300,
input_larrow_down() { this.input_a_down(); },
frame_vel: [0,0],
input_a_down() {
this.frame_vel = this.frame_vel.add([-1,0]);
},
input_d_down() {
this.frame_vel = this.frame_vel.add([1,0]);
},
update(dt) {
this.angle = 0;
var fpos = this.pos;
fpos.x += this.frame_vel.x * this.speed * dt;
fpos.x = Math.clamp(fpos.x,-this.max_x,this.max_x);
this.pos = fpos;
this.frame_vel = [0,0];
if (this.stickball)
this.stickball.pos = this.pos.add(this.stickpos);
},
physupdate(dt) { this.velocity = [0,0]; },
gamepad_ljoy_axis(v) {
v[1] = 0;
this.pos = this.pos.add(v.scale(this.speed*Yugine.dt));
},
setup() {
Player.players[0].control(this);
this.length = this.lengths[0];
},
grow() {
this.setgrow(this.growlvl + 1);
},
setgrow(n) {
this.growlvl = Math.clamp(n,1,this.maxgrow);
this.length = this.lengths[this.growlvl];
this.img.path = "pill" + this.growlvl + ".png";
this.img.sync();
var d = this.img.dimensions;
this.collider.points = Geometry.box(d.x, d.y);
this.collider.sync();
this.max_x = this.extents - (d.x*this.scale)/2;
},
lasership() {
this.laser = true;
},
shoot() {
Log.warn("SHOOTING MISSILES");
},
sticky: false,
stored_vel: [0,0],
stickball: {},
stickpos: [0,0],
input_space_pressed() {
if (this.sticky && this.stickball) {
this.stickball.velocity = this.stored_vel;
this.stickball = {};
}
if (this.laser) {
this.shoot();
}
},
collide(hit) {
if (!(hit.obj.from === 'ball')) return;
var xdiff = hit.pos.x - this.pos.x;
var hitangle = xdiff/this.length - 0.5;
var speed = Vector.length(hit.obj.velocity);
hit.obj.velocity = [Math.cos(hitangle)*speed, Math.sin(hitangle)*speed];
hit.obj.velocity = hit.obj.velocity.scale(-1);
var oldvel = hit.obj.velocity;
var scaler = Math.max(Math.abs(xdiff)/30, 1.01);
hit.obj.velocity = hit.obj.velocity.scale(scaler);
if (this.sticky) {
this.stored_vel = hit.obj.velocity;
hit.obj.velocity = [0,0];
this.stickball = hit.obj;
this.stickpos = hit.obj.pos.sub(this.pos);
}
},
});
gameobject.clone("upgrade_drop", {
fallspeed: 150,
tag: "ball",
upgrade: function() {},
size: 12,
phys:1,
collider: circle2d.clone(),
color: Color.Rainbow.blue,
start() {
this.collider.radius = this.size;
this.collider.sensor = true;
},
draw() {
Shape.circle(this.pos,this.size,this.color);
},
collide(hit) {
if (!(hit.obj.from === 'paddle'))
return;
this.upgrade();
this.kill();
},
update(dt) {
this.pos = this.pos.add([0,-this.fallspeed*dt]);
},
});