You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zomburger/public/player.js

112 lines
2.5 KiB
JavaScript

class Player {
constructor(options) {
options = options || {};
5 years ago
this.game = options.game;
this.id = options.id;
this.image = random(Images.players);
this.width = options.width || 64;
this.height = options.height || 64;
5 years ago
this.burgers = 5;
5 years ago
this.maxBurgers = 12;
this.moneys = 0;
this.maxMoneys = 20;
this.score = 0;
this.position = {x: options.x, y: options.y};
this.velocity = {x: 0, y: 0};
}
update() {
let dt = deltaTime / 1000;
this.position.x += this.velocity.x * dt;
this.position.y += this.velocity.y * dt;
}
draw() {
let corner = {
x: this.position.x - this.width*0.5,
y: this.position.y - this.height*0.5
};
5 years ago
let half = {
width: this.width * 0.5,
height: this.height * 0.5,
};
tint(Colors.Purple);
image(this.image, corner.x, corner.y, this.width, this.height);
5 years ago
this.drawBurgerMeter();
this.drawMoneyMeter();
if (debug) {
strokeWeight(4);
stroke('#FF0000');
point(this.position.x, this.position.y);
strokeWeight(1);
noFill();
rect(corner.x, corner.y, this.width, this.height);
}
}
5 years ago
drawBurgerMeter() {
noStroke();
fill(Colors.Purple);
let meterWidth = this.width;
let meterHeight = this.height * 0.2;
let cellWidth = meterWidth / this.maxBurgers;
for (let i = 0; i < this.burgers; i++) {
let x = this.position.x - this.width * 0.5 + i * cellWidth;
let y = this.position.y + this.height + 10;
rect(x + cellWidth * 0.1, y, cellWidth*0.8, 10);
}
}
drawMoneyMeter() {
noStroke();
fill(Colors.DarkGreen);
let meterWidth = this.width;
let meterHeight = this.height * 0.2;
let cellWidth = meterWidth / this.maxMoneys;
for (let i = 0; i < this.moneys; i++) {
let x = this.position.x - this.width * 0.5 + i * cellWidth;
let y = this.position.y + this.height + 30;
rect(x + cellWidth * 0.1, y, cellWidth*0.8, 10);
}
}
kill() {
// something here
}
joystickInput(x, y) {
this.velocity.x = x * 200;
this.velocity.y = y * -200;
}
5 years ago
5 years ago
buyBurger() {
if (this.moneys > 0 && (this.burgers+2) <= this.maxBurgers) {
this.moneys--;
this.burgers+=2;
}
}
makeDeposit() {
if (this.moneys > 0) {
this.moneys--;
this.score++;
}
}
throwBurger() {
if (this.burgers > 0) {
this.burgers--;
this.game.addBurger(this.id);
} else {
5 years ago
}
}
}
Player.prototype.bounds = bounds;
Player.prototype.outOfBounds = outOfBounds;
Player.prototype.overlaps = overlaps;