Split out objects into own scripts

This commit is contained in:
John Alanbrook 2023-09-07 21:46:56 +00:00
parent 996dd96d50
commit 3b67b08683
9 changed files with 238 additions and 238 deletions

12
ball.js Normal file
View file

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

View file

@ -332,7 +332,7 @@ function gamegui() {
}).draw(Window.dimensions.scale([0.8,0.8]));
}
Register.gui.register(gamegui);
Register.gui.register(gamegui, this);
Player.players[0].control(gamepawn);

30
brick.js Normal file
View file

@ -0,0 +1,30 @@
Object.assign(self, {
collider: polygon2d.clone(),
char: char2d.clone({
flash:Resources.load("brick.png"),
}),
points: 50,
getshot() {
this.kill();
},
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();
},
});

233
config.js
View file

@ -1,17 +1,5 @@
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],
@ -27,224 +15,3 @@ Color.Rainbow = {
green: [0,153,1],
blue: [53,51,255]
};
gameobject.clone("brick", {
collider: polygon2d.clone(),
char: char2d.clone({
flash:Resources.load("brick.png"),
}),
points: 50,
getshot() {
this.kill();
},
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");
gameobject.clone("missile", {
collider: polygon2d.clone(),
phys: 1,
bwidth: 5,
bheight: 15,
speed: 200,
start() {
this.collider.points = Geometry.box(this.bwidth, this.bheight);
this.collider.sensor = true;
},
collide(hit) {
if ('getshot' in hit.obj) {
hit.obj.getshot();
this.kill();
}
},
draw() {
Debug.box(this.pos, [this.bwidth, this.bheight], Color.cyan);
},
update(dt) {
this.pos = this.pos.add([0,this.speed*dt]);
},
});
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,
frame_vel: [0,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;
this.img.color = Color.Gameboy.light;
},
unlasership() {
this.laser = false;
this.img.color = Color.white;
},
shoot() {
var m1 = World.spawn(gameobjects['missile']);
var m2 = World.spawn(gameobjects['missile']);
m1.pos = this.pos.add([(this.length/2)-5,0]);
m2.pos = this.pos.add([5-(this.length/2),0]);
},
sticky: false,
stored_vel: [0,0],
stickball: {},
stickpos: [0,0],
start() {
this.collider.sensor = true;
},
collide(hit) {
if (!(hit.obj.from === 'ball')) return;
var xdiff = hit.pos.x - this.pos.x;
var hitangle = (xdiff/this.length + 0.5) * Math.PI;
hitangle = Math.clamp(hitangle, Math.PI/6, 5*Math.PI/6);
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);
}
},
});
paddle.inputs = {};
paddle.inputs.space = function() {
if (this.sticky && this.stickball) {
this.stickball.velocity = this.stored_vel;
this.stickball = {};
}
if (this.laser) {
this.shoot();
}
};
paddle.inputs.a = function(){};
paddle.inputs.a.down = function() {
this.frame_vel = this.frame_vel.add([-1,0]);
};
paddle.inputs.d = {};
paddle.inputs.d.down = function() {
this.frame_vel = this.frame_vel.add([1,0]);
};
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]);
},
});

View file

@ -1,2 +1,6 @@
//var b = prototypes.from_file("ball.js");
//var ball1 = b.make();
//Log.warn(ball1.from);
//var ball2 = ball1.dup({pos:[100,100]});
Window.icon("powerup.png");
World.load("startmenu");

29
missile.js Normal file
View file

@ -0,0 +1,29 @@
var def = {
collider: polygon2d.clone(),
phys: 1,
bwidth: 5,
bheight: 15,
speed: 200,
start() {
this.collider.points = Geometry.box(this.bwidth, this.bheight);
this.collider.sensor = true;
},
collide(hit) {
if ('getshot' in hit.obj) {
hit.obj.getshot();
this.kill();
}
},
draw() {
Debug.box(this.pos, [this.bwidth, this.bheight], Color.cyan);
},
update(dt) {
this.pos = this.pos.add([0,this.speed*dt]);
},
};
Object.assign(self, def);

126
paddle.js Normal file
View file

@ -0,0 +1,126 @@
Object.assign(self, {
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,
frame_vel: [0,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() {
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;
this.img.color = Color.Gameboy.light;
},
unlasership() {
this.laser = false;
this.img.color = Color.white;
},
shoot() {
var m1 = World.spawn(gameobjects['missile']);
var m2 = World.spawn(gameobjects['missile']);
m1.pos = this.pos.add([(this.length/2)-5,0]);
m2.pos = this.pos.add([5-(this.length/2),0]);
},
sticky: false,
stored_vel: [0,0],
stickball: {},
stickpos: [0,0],
start() {
this.collider.sensor = true;
},
collide(hit) {
if (!(hit.obj.from === 'ball')) return;
var xdiff = hit.pos.x - this.pos.x;
var hitangle = (xdiff/this.length + 0.5) * Math.PI;
hitangle = Math.clamp(hitangle, Math.PI/6, 5*Math.PI/6);
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);
}
},
});
self.inputs = {};
self.inputs.space = function() {
if (this.sticky && this.stickball) {
this.stickball.velocity = this.stored_vel;
this.stickball = {};
}
if (this.laser) {
this.shoot();
}
};
self.inputs.a = function(){};
self.inputs.a.down = function() {
this.frame_vel = this.frame_vel.add([-1,0]);
};
self.inputs.d = {};
self.inputs.d.down = function() {
this.frame_vel = this.frame_vel.add([1,0]);
};

View file

@ -1,9 +1,11 @@
var startgame = function()
{
self.kill();
run("breakout.js");
scene.kill();
// Player.players[0].uncontrol(startcontroller);
//Register.gui.unregister(startmenu,this);
// Register.gui.unregister_obj(this);
};
var exitgame = function()
@ -71,7 +73,7 @@ function startmenu() {
GUI.image_fn({path:"coin.png", anchor: [1,0.5]}).draw([item.bb.l, (item.bb.b + item.bb.t)/2].add([-3,1]));
}
Register.gui.register(startmenu, scene);
Register.gui.register(startmenu, this);
var startcontroller = {};
startcontroller.inputs = {};
@ -89,7 +91,7 @@ startcontroller.inputs.w = function() {
item.selected = true;
};
startcontroller.inputs.enter = function() { Log.warn("TEST"); item.action(); };
startcontroller.inputs.enter = function() { item.action(); };
Player.players[0].control(startcontroller);
Game.play();

30
upgrade_drop.js Normal file
View file

@ -0,0 +1,30 @@
Object.assign(self, {
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]);
},
});