diff --git a/src/player.js b/src/player.js new file mode 100644 index 0000000..f377946 --- /dev/null +++ b/src/player.js @@ -0,0 +1,26 @@ +// Seed #2: the white pixel cat. Arrows/WASD to move, Space/Up to jump, E/Down at a door. +import { blit } from "./sprites.js"; +import { solidBelow } from "./world.js"; +export const SPEED = 82, JUMP = 205, GRAV = 560; +export function makePlayer() { + return { + x: 24, y: 40, vx: 0, vy: 0, f: 1, onGround: false, walk: 0, emote: null, emoteT: 0, jumps: 1, + update(dt, w, input) { + const dir = (input.held("right") ? 1 : 0) - (input.held("left") ? 1 : 0); + this.vx = dir * SPEED; if (dir) this.f = dir; + this.walk = dir && this.onGround ? this.walk + dt : 0; + if (input.take("jump") && (this.onGround || this.jumpsLeft > 0)) { if (!this.onGround) this.jumpsLeft--; this.vy = -JUMP; this.onGround = false; } + for (let i = 0; i < 4; i++) if (input.take("e" + i)) { this.emote = i; this.emoteT = 2; } + this.emoteT = Math.max(0, this.emoteT - dt); if (!this.emoteT) this.emote = null; + this.vy = Math.min(320, this.vy + GRAV * dt); + const ny = this.y + this.vy * dt; + const floor = this.vy >= 0 ? solidBelow(w.level, this.x, this.y - 0.01, ny) : null; + if (floor != null) { this.y = floor; this.vy = 0; this.onGround = true; this.jumpsLeft = this.jumps - 1; } else { this.y = ny; this.onGround = false; } + this.x += this.vx * dt; + }, + draw(g) { + blit(g, Math.floor(this.walk * 8) % 2 ? "catWalk" : "cat", this.x - 6, this.y - 11, this.f < 0); + if (this.emote != null) blit(g, ["heart", "wave", "bang", "ask"][this.emote], this.x - 2, this.y - 19); + }, + }; +}