"use strict" class Player{ constructor(x, y){ this.pos = createVector(x, y); this.vel = createVector(0, 0); this.acc = createVector(0, 0.25); this.radius = 50; this.brick = null; this.colorSetup(); } update(){ this.move(); } display(){ this.show(); } move(){ if (this.brick){ this.vel.y = 0; this.pos.add(this.brick.vel); this.pos.y = this.brick.pos.y - this.radius; } this.pos.add(this.vel); this.vel.add(this.acc); this.vel.x *= 0.95; let limit = this.maxVelocityX; if (this.vel.x < -limit) this.vel.x = -limit; if (this.vel.x > limit) this.vel.x = limit; } show(){ stroke(red(this.color) * 0.5, green(this.color) * 0.5, blue(this.color) * 0.5); strokeWeight(1); //Body fill(this.color); ellipse(this.pos.x, this.pos.y, this.radius * 2); //Eyes let radius = this.radius * 0.3; let dispoX = 1 / (1 + pow(0.7, this.vel.x)) - 0.5; let dispoY = 1 / (1 + pow(0.7, this.vel.y)) - 0.5; let outerDispoX = dispoX * this.radius / 2; let outerDispoY = dispoY * this.radius / 2; let innerDispoX = outerDispoX + dispoX * radius / 2; let innerDispoY = outerDispoY + dispoY * radius / 2; fill(colors.player.eyes.outer); ellipse(this.pos.x + radius + outerDispoX, this.pos.y - radius + outerDispoY, radius, radius * 4/3); ellipse(this.pos.x - radius + outerDispoX, this.pos.y - radius + outerDispoY, radius, radius * 4/3); fill(colors.player.eyes.inner); ellipse(this.pos.x + radius + innerDispoX, this.pos.y - radius + innerDispoY, radius / 2, radius * 2/3); ellipse(this.pos.x - radius + innerDispoX, this.pos.y - radius + innerDispoY, radius / 2, radius * 2/3); //Mouth let rotation = dispoX / 2; fill(red(this.color) / 3, green(this.color) / 3, blue(this.color) / 3); arc(this.pos.x, this.pos.y + this.radius * 0.2 + dispoY * this.radius / 2, this.radius, this.radius * 0.8, PI / 16 + rotation, PI - PI / 16 + rotation, CHORD); } collision(bricks, world){ //Bricks if (!this.touches(this.brick)){ let brick = null; for (let b of bricks){ if (this.touches(b)){ brick = b; break; } else brick = null; } this.brick = brick; } let trashHold = 10; //World borders if (this.pos.x - this.radius < world.pos.x) this.pos.x = world.pos.x + this.radius; if (this.pos.x + this.radius > world.pos.x + world.dim.x) this.pos.x = world.pos.x + world.dim.x - this.radius; if (this.pos.y - this.radius < world.pos.y){ this.pos.y = world.pos.y + this.radius; this.vel.y *= -1; } if (this.pos.y - this.radius > world.pos.y + world.dim.y) game.lost(); } touches(object){ if (!object) return false; let hitbox = object.getHitbox(); if (object instanceof Brick){ if (this.pos.x + this.radius / 2 > hitbox.pos.x && this.pos.x - this.radius / 2 < hitbox.pos.x + hitbox.dim.x && (this.pos.y + this.radius <= hitbox.pos.y && (this.pos.y + this.radius + this.vel.y >= hitbox.pos.y + hitbox.vel.y || this.pos.y + this.radius + this.vel.y >= hitbox.pos.y))){ return true; } } if (object instanceof Coin){ let distX = abs(this.pos.x - (hitbox.pos.x + hitbox.dim.x / 2)); let distY = abs(this.pos.y - (hitbox.pos.y + hitbox.dim.y / 2)); if ((distX <= (hitbox.dim.x / 2) || distY <= (hitbox.dim.y / 2)) && distX < (hitbox.dim.x / 2 + this.radius) && distY < (hitbox.dim.y / 2 + this.radius)) return true; let dx = distX - hitbox.dim.x / 2; let dy = distY - hitbox.dim.y / 2; if (dx * dx + dy * dy <= this.radius * this.radius) return true; } return false; } walk(d){ this.acc.x = d; } break(){ this.acc.x = 0; } jump(){ if (!this.brick) return; this.vel.y = -abs(this.vel.x * 0.3) - this.jumpVelocity + this.brick.vel.y; this.brick = null; new Sound(Sound.Jump()).play(); } fall(d){ this.acc.y += d * 0.5; } setColor(color){ this.color = color; } colorSetup(){ let h = int(random(360)); h = floor(h / 60) * 60; let c = color(colors.player.body); colorMode(HSB); this.color = color(h, saturation(c), brightness(c)); colorMode(RGB); } }