player.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. Blocky - player.js
  3. Player movement and survival stats.
  4. Movement: walking, sprinting, sneaking (which keeps you from walking off
  5. edges), jumping, swimming, ladder climbing and creative flight, with a
  6. 0.6 block auto-step so slabs and stairs can be walked up, and a snap
  7. down onto a step up to 0.6 below so they can be walked down. Steps
  8. move the camera smoothly (eyeSmooth). Swimming against a bank pushes
  9. you out once its top is within reach.
  10. Survival: 20 health points, 20 food points with a hidden saturation
  11. buffer and exhaustion counter. Exhaustion comes from actions and from a
  12. slow constant drain whose speed depends on the difficulty. A full food
  13. bar regenerates health; an empty one starves you (how far depends on the
  14. difficulty again).
  15. */
  16. import { B, isOpaqueAt } from "./blocks.js";
  17. import { moveAxis, regionBlocked, gatherBoxes } from "./physics.js";
  18. import { waterHeight } from "./mesher.js";
  19. import { clamp } from "./util.js";
  20. export const DIFFICULTY = [
  21. //drain: exhaustion per second while idle; mult: multiplier on action exhaustion
  22. { name: "Peaceful", drain: 0, mult: 0, starveFloor: 20 },
  23. { name: "Easy", drain: 0.012, mult: 0.75, starveFloor: 10 },
  24. { name: "Normal", drain: 0.025, mult: 1.0, starveFloor: 1 },
  25. { name: "Hard", drain: 0.045, mult: 1.5, starveFloor: 0 }
  26. ];
  27. const HALF_W = 0.3, HEIGHT = 1.8, EYE = 1.62, STEP = 0.6;
  28. //Lava kills from full health on the 7th hit, just under 4 seconds in
  29. const LAVA_DAMAGE = 3, LAVA_INTERVAL = 0.6;
  30. export class Player {
  31. constructor(world) {
  32. this.world = world;
  33. this.x = 0; this.y = 80; this.z = 0;
  34. this.vx = 0; this.vy = 0; this.vz = 0;
  35. this.yaw = 0; this.pitch = 0;
  36. this.onGround = false;
  37. this.inWater = false;
  38. this.headInWater = false;
  39. this.onLadder = false;
  40. this.flying = false;
  41. this.sneaking = false;
  42. //Shift is down, also while flying (where it sinks instead of sneaks)
  43. this.shiftHeld = false;
  44. this.sprinting = false;
  45. this.horizontalHit = false;
  46. this.fallDistance = 0;
  47. this.walkDist = 0;
  48. this.eyeSmooth = 0;
  49. this.creative = false;
  50. this.difficulty = 2;
  51. this.health = 20;
  52. this.food = 20;
  53. this.saturation = 5;
  54. this.exhaustion = 0;
  55. this.air = 15;
  56. this.regenTimer = 0;
  57. this.starveTimer = 0;
  58. this.drownTimer = 0;
  59. this.cactusTimer = 0;
  60. this.fireTimer = 0;
  61. this.burnTime = 0;
  62. this.burnTick = 0;
  63. this.hurtTime = 0;
  64. this.dead = false;
  65. this.events = [];
  66. }
  67. get eyeY() {
  68. return this.y + EYE - (this.sneaking && !this.flying ? 0.12 : 0);
  69. }
  70. box() {
  71. return [this.x - HALF_W, this.y, this.z - HALF_W, this.x + HALF_W, this.y + HEIGHT, this.z + HALF_W];
  72. }
  73. setPos(x, y, z) {
  74. this.x = x; this.y = y; this.z = z;
  75. this.vx = this.vy = this.vz = 0;
  76. this.fallDistance = 0;
  77. this.eyeSmooth = 0;
  78. }
  79. //Is the given world point under the surface of a liquid (water or lava)?
  80. waterAt(px, py, pz, liquid = B.WATER) {
  81. const w = this.world;
  82. const x = Math.floor(px), y = Math.floor(py), z = Math.floor(pz);
  83. if (w.getBlock(x, y, z) !== liquid) {
  84. return false;
  85. }
  86. const h = waterHeight(w.getMeta(x, y, z), w.getBlock(x, y + 1, z) === liquid);
  87. return py - y < h + 0.02;
  88. }
  89. //Swimming against a bank: would the spot 0.6 higher and a little ahead
  90. //(along wx, wz) be free of blocks and above any liquid? Then the bank
  91. //can be climbed, even one level with the water surface.
  92. canClimbOut(wx, wz) {
  93. const b = this.box();
  94. const ox = wx * 0.2, oz = wz * 0.2, lift = 0.6;
  95. if (regionBlocked(this.world, b[0] + ox, b[1] + lift, b[2] + oz, b[3] + ox, b[4] + lift, b[5] + oz)) {
  96. return false;
  97. }
  98. const fx = this.x + ox, fy = this.y + lift, fz = this.z + oz;
  99. return !this.waterAt(fx, fy, fz) && !this.waterAt(fx, fy, fz, B.LAVA);
  100. }
  101. sense() {
  102. const w = this.world;
  103. this.inLava = this.waterAt(this.x, this.y + 0.25, this.z, B.LAVA) || this.waterAt(this.x, this.y + 0.9, this.z, B.LAVA);
  104. this.headInLava = this.waterAt(this.x, this.eyeY, this.z, B.LAVA);
  105. this.inWater = !this.inLava && (this.waterAt(this.x, this.y + 0.25, this.z) || this.waterAt(this.x, this.y + 0.9, this.z));
  106. this.headInWater = this.waterAt(this.x, this.eyeY, this.z);
  107. this.onLadder = false;
  108. const b = this.box();
  109. for (let x = Math.floor(b[0]); x <= Math.floor(b[3]); x++) {
  110. for (let z = Math.floor(b[2]); z <= Math.floor(b[5]); z++) {
  111. for (let y = Math.floor(b[1]); y <= Math.floor(b[4]); y++) {
  112. if (w.getBlock(x, y, z) === B.LADDER) {
  113. this.onLadder = true;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. //input: forward, strafe (-1..1), jump, sneak, sprint (bools)
  120. update(dt, input) {
  121. this.sense();
  122. const w = this.world;
  123. if (!this.creative) {
  124. this.flying = false;
  125. }
  126. this.shiftHeld = !!input.sneak;
  127. this.sneaking = this.shiftHeld && !this.flying;
  128. const canSprint = this.creative || this.food > 6 || this.difficulty === 0;
  129. if (input.sprint && input.forward > 0 && !this.sneaking && canSprint) {
  130. this.sprinting = true;
  131. }
  132. if (input.forward <= 0 || this.sneaking || !canSprint || (this.horizontalHit && !this.flying)) {
  133. this.sprinting = false;
  134. }
  135. //Horizontal intent
  136. let fx = input.forward, sx = input.strafe;
  137. const len = Math.hypot(fx, sx);
  138. if (len > 1) {
  139. fx /= len;
  140. sx /= len;
  141. }
  142. const sin = Math.sin(this.yaw), cos = Math.cos(this.yaw);
  143. const wx = -sin * fx + cos * sx;
  144. const wz = -cos * fx - sin * sx;
  145. let speed;
  146. if (this.flying) {
  147. speed = this.sprinting ? 21.6 : 10.9;
  148. } else if (this.inLava) {
  149. speed = 1.1;
  150. } else if (this.inWater) {
  151. speed = this.sprinting ? 3.2 : 2.2;
  152. } else if (this.sneaking) {
  153. speed = 1.3;
  154. } else {
  155. speed = this.sprinting ? 5.6 : 4.3;
  156. }
  157. let accel;
  158. if (this.flying) {
  159. accel = 7;
  160. } else if (this.inWater || this.inLava) {
  161. accel = 6;
  162. } else if (this.onGround) {
  163. accel = 16;
  164. } else {
  165. accel = 2.6;
  166. }
  167. const k = 1 - Math.exp(-accel * dt);
  168. this.vx += (wx * speed - this.vx) * k;
  169. this.vz += (wz * speed - this.vz) * k;
  170. //Vertical
  171. if (this.flying) {
  172. const ty = (input.jump ? 8.5 : 0) - (input.sneak ? 8.5 : 0);
  173. this.vy += (ty - this.vy) * (1 - Math.exp(-10 * dt));
  174. } else if (this.inLava) {
  175. //Thick and slow: sinks gently, swims up weakly
  176. this.vy -= 6 * dt;
  177. this.vy *= Math.exp(-3.5 * dt);
  178. if (input.jump) {
  179. this.vy = Math.min(this.vy + 14 * dt, 1.8);
  180. if (this.horizontalHit) {
  181. this.vy = this.canClimbOut(wx, wz) ? 6 : 3.5;
  182. }
  183. }
  184. this.vy = Math.max(this.vy, -1.8);
  185. } else if (this.inWater) {
  186. this.vy -= 10 * dt;
  187. this.vy *= Math.exp(-2.2 * dt);
  188. if (input.jump) {
  189. this.vy = Math.min(this.vy + 26 * dt, 3.2);
  190. //Against a bank: 4.5 alone tops out just under a bank level
  191. //with the surface, so once its top is in reach push harder
  192. if (this.horizontalHit) {
  193. this.vy = this.canClimbOut(wx, wz) ? 6.5 : 4.5;
  194. }
  195. }
  196. this.vy = Math.max(this.vy, -3.5);
  197. } else {
  198. this.vy -= 32 * dt;
  199. this.vy = Math.max(this.vy, -60);
  200. if (input.jump && this.onGround) {
  201. this.vy = 9.0;
  202. if (this.sprinting) {
  203. this.vx += -sin * 2.0;
  204. this.vz += -cos * 2.0;
  205. this.exhaust(0.2);
  206. } else {
  207. this.exhaust(0.05);
  208. }
  209. }
  210. }
  211. if (this.onLadder && !this.flying) {
  212. this.fallDistance = 0;
  213. this.vy = Math.max(this.vy, -2.4);
  214. if (this.horizontalHit || input.jump) {
  215. this.vy = 2.4;
  216. } else if (this.sneaking) {
  217. this.vy = Math.max(this.vy, 0);
  218. }
  219. }
  220. //Move and collide
  221. const box = this.box();
  222. const dx = this.vx * dt, dy = this.vy * dt, dz = this.vz * dt;
  223. const wasGround = this.onGround;
  224. const my = moveAxis(w, box, 1, dy);
  225. this.onGround = dy < 0 && my > dy + 1e-6;
  226. if (my !== dy) {
  227. this.vy = 0;
  228. }
  229. let mx = dx, mz = dz;
  230. //Sneaking keeps you on the block you stand on
  231. if (this.sneaking && this.onGround) {
  232. const below = (b, ox, oz) => regionBlocked(w, b[0] + ox, b[1] - 0.6, b[2] + oz, b[3] + ox, b[1] - 0.001, b[5] + oz);
  233. if (mx !== 0 && !below(box, mx, 0)) {
  234. mx = 0;
  235. this.vx = 0;
  236. }
  237. if (mz !== 0 && !below(box, mx, mz)) {
  238. mz = 0;
  239. this.vz = 0;
  240. }
  241. }
  242. const start = box.slice();
  243. const rx = moveAxis(w, box, 0, mx);
  244. const rz = moveAxis(w, box, 2, mz);
  245. let hitX = Math.abs(rx - mx) > 1e-6, hitZ = Math.abs(rz - mz) > 1e-6;
  246. //Auto step for half blocks
  247. if ((hitX || hitZ) && (this.onGround || wasGround) && !this.flying) {
  248. const alt = start.slice();
  249. const up = moveAxis(w, alt, 1, STEP);
  250. const ax = moveAxis(w, alt, 0, mx);
  251. const az = moveAxis(w, alt, 2, mz);
  252. moveAxis(w, alt, 1, -up);
  253. if (ax * ax + az * az > rx * rx + rz * rz + 1e-6) {
  254. this.eyeSmooth -= alt[1] - box[1];
  255. for (let i = 0; i < 6; i++) {
  256. box[i] = alt[i];
  257. }
  258. hitX = Math.abs(ax - mx) > 1e-6;
  259. hitZ = Math.abs(az - mz) > 1e-6;
  260. }
  261. }
  262. if (hitX) {
  263. this.vx = 0;
  264. }
  265. if (hitZ) {
  266. this.vz = 0;
  267. }
  268. this.horizontalHit = hitX || hitZ;
  269. //Walked off a step (stairs, a slab): land on the one below right away
  270. //instead of falling, so walking downstairs keeps you on the ground
  271. if (wasGround && !this.onGround && this.vy <= 0 && !input.jump && !this.flying && !this.inWater && !this.inLava && !this.onLadder) {
  272. const probe = box.slice();
  273. const down = moveAxis(w, probe, 1, -STEP);
  274. if (down > -STEP + 1e-6) {
  275. this.eyeSmooth -= down;
  276. for (let i = 0; i < 6; i++) {
  277. box[i] = probe[i];
  278. }
  279. this.onGround = true;
  280. this.vy = 0;
  281. }
  282. }
  283. //The camera follows steps smoothly
  284. this.eyeSmooth = clamp(this.eyeSmooth * Math.exp(-18 * dt), -0.6, 0.6);
  285. if (Math.abs(this.eyeSmooth) < 1e-3) {
  286. this.eyeSmooth = 0;
  287. }
  288. //Holding sneak while flying into the ground lands you
  289. if (this.flying && this.onGround && input.sneak) {
  290. this.flying = false;
  291. this.vy = 0;
  292. }
  293. const oldY = this.y;
  294. this.x = (box[0] + box[3]) / 2;
  295. this.y = box[1];
  296. this.z = (box[2] + box[5]) / 2;
  297. //Distance based effects
  298. const moved = Math.hypot(this.x - (start[0] + start[3]) / 2, this.z - (start[2] + start[5]) / 2);
  299. if (this.onGround || this.inWater) {
  300. this.walkDist += moved;
  301. }
  302. if (!this.flying) {
  303. if (this.inWater) {
  304. this.exhaust(0.012 * moved);
  305. } else if (this.sprinting) {
  306. this.exhaust(0.1 * moved);
  307. }
  308. }
  309. //Falling
  310. if (this.flying || this.inWater || this.inLava || this.onLadder) {
  311. this.fallDistance = 0;
  312. } else if (this.y < oldY) {
  313. this.fallDistance += oldY - this.y;
  314. }
  315. if (this.onGround && !wasGround) {
  316. if (this.fallDistance > 3 && !this.creative) {
  317. const dmg = Math.floor(this.fallDistance - 3);
  318. if (dmg > 0) {
  319. this.hurt(dmg, "fall");
  320. }
  321. }
  322. this.events.push({ type: "land", fall: this.fallDistance });
  323. this.fallDistance = 0;
  324. }
  325. if (this.onGround) {
  326. this.fallDistance = 0;
  327. }
  328. if (this.y < -40) {
  329. this.hurt(4, "void");
  330. this.vy = Math.max(this.vy, 0);
  331. }
  332. }
  333. /* ---------------- survival ---------------- */
  334. exhaust(v) {
  335. if (this.creative) {
  336. return;
  337. }
  338. this.exhaustion += v * DIFFICULTY[this.difficulty].mult;
  339. }
  340. hurt(amount, cause) {
  341. if (this.creative || this.dead || amount <= 0) {
  342. return;
  343. }
  344. if (this.hurtTime > 0 && cause !== "starve" && cause !== "drown") {
  345. return;
  346. }
  347. this.health = Math.max(0, this.health - amount);
  348. this.hurtTime = 0.5;
  349. this.exhaust(0.1);
  350. this.events.push({ type: "hurt", amount, cause });
  351. if (this.health <= 0) {
  352. this.dead = true;
  353. this.events.push({ type: "death", cause });
  354. }
  355. }
  356. kill(cause) {
  357. if (this.dead) {
  358. return;
  359. }
  360. this.health = 0;
  361. this.dead = true;
  362. this.events.push({ type: "death", cause });
  363. }
  364. eat(food) {
  365. this.food = Math.min(20, this.food + food.hunger);
  366. this.saturation = Math.min(this.food, this.saturation + food.sat);
  367. }
  368. //Called 20 times a second
  369. tick() {
  370. const dt = 0.05;
  371. if (this.hurtTime > 0) {
  372. this.hurtTime = Math.max(0, this.hurtTime - dt);
  373. }
  374. if (this.creative || this.dead) {
  375. this.air = 15;
  376. return;
  377. }
  378. const diff = DIFFICULTY[this.difficulty];
  379. //Constant drain, scaled by difficulty
  380. this.exhaustion += diff.drain * dt;
  381. while (this.exhaustion >= 4) {
  382. this.exhaustion -= 4;
  383. if (this.saturation > 0) {
  384. this.saturation = Math.max(0, this.saturation - 1);
  385. } else {
  386. this.food = Math.max(0, this.food - 1);
  387. }
  388. }
  389. if (this.difficulty === 0) {
  390. this.food = 20;
  391. this.regenTimer += dt;
  392. if (this.regenTimer >= 1 && this.health < 20) {
  393. this.regenTimer = 0;
  394. this.health = Math.min(20, this.health + 1);
  395. }
  396. } else if (this.food >= 18 && this.health < 20) {
  397. this.regenTimer += dt;
  398. if (this.regenTimer >= 4) {
  399. this.regenTimer = 0;
  400. this.health = Math.min(20, this.health + 1);
  401. this.exhaustion += 3;
  402. }
  403. } else {
  404. this.regenTimer = 0;
  405. }
  406. if (this.food <= 0) {
  407. this.starveTimer += dt;
  408. if (this.starveTimer >= 4) {
  409. this.starveTimer = 0;
  410. if (this.health > diff.starveFloor) {
  411. this.hurt(1, "starve");
  412. }
  413. }
  414. } else {
  415. this.starveTimer = 0;
  416. }
  417. //Breath
  418. if (this.headInWater) {
  419. this.air -= dt;
  420. if (this.air <= -1) {
  421. this.air = 0;
  422. this.hurt(2, "drown");
  423. }
  424. } else {
  425. this.air = Math.min(15, this.air + dt * 5);
  426. }
  427. //Cactus prickles (slowly: half a heart a second); a block in your
  428. //head (fallen sand) suffocates
  429. this.cactusTimer = Math.max(0, this.cactusTimer - dt);
  430. if (this.cactusTimer === 0 && this.touching(B.CACTUS)) {
  431. this.cactusTimer = 1;
  432. this.hurt(1, "cactus");
  433. }
  434. //Fire and lava: damage while touching, then you keep burning a while
  435. this.fireTimer = Math.max(0, this.fireTimer - dt);
  436. if (this.inLava) {
  437. this.burnTime = 8;
  438. if (this.fireTimer === 0) {
  439. this.fireTimer = LAVA_INTERVAL;
  440. this.hurt(LAVA_DAMAGE, "lava");
  441. }
  442. } else if (this.touching(B.FIRE)) {
  443. this.burnTime = Math.max(this.burnTime, 4);
  444. if (this.fireTimer === 0) {
  445. this.fireTimer = 0.5;
  446. this.hurt(1, "fire");
  447. }
  448. }
  449. if (this.burnTime > 0) {
  450. const rained = this.world.raining && this.world.rainTop(Math.floor(this.x), Math.floor(this.z)) <= this.y + 0.5;
  451. if (this.inWater || rained) {
  452. this.burnTime = 0;
  453. this.events.push({ type: "extinguish" });
  454. } else {
  455. this.burnTime = Math.max(0, this.burnTime - dt);
  456. this.burnTick += dt;
  457. if (this.burnTick >= 1 && !this.inLava) {
  458. this.burnTick = 0;
  459. this.hurt(1, "burn");
  460. }
  461. }
  462. }
  463. const hx = Math.floor(this.x), hy = Math.floor(this.eyeY), hz = Math.floor(this.z);
  464. if (this.world.isLoaded(hx, hz) && isOpaqueAt(this.world.getBlock(hx, hy, hz), this.world.getMeta(hx, hy, hz))) {
  465. this.suffocateTimer = (this.suffocateTimer || 0) + dt;
  466. if (this.suffocateTimer >= 0.5) {
  467. this.suffocateTimer = 0;
  468. this.hurt(1, "suffocate");
  469. }
  470. } else {
  471. this.suffocateTimer = 0;
  472. }
  473. }
  474. touching(id) {
  475. const b = this.box();
  476. const e = 0.05;
  477. for (let x = Math.floor(b[0] - e); x <= Math.floor(b[3] + e); x++) {
  478. for (let z = Math.floor(b[2] - e); z <= Math.floor(b[5] + e); z++) {
  479. for (let y = Math.floor(b[1] - e); y <= Math.floor(b[4]); y++) {
  480. if (this.world.getBlock(x, y, z) !== id) {
  481. continue;
  482. }
  483. const cx0 = x + 0.0625, cx1 = x + 0.9375, cz0 = z + 0.0625, cz1 = z + 0.9375;
  484. if (b[0] - e < cx1 && b[3] + e > cx0 && b[2] - e < cz1 && b[5] + e > cz0 && b[1] - e < y + 1 && b[4] > y) {
  485. return true;
  486. }
  487. }
  488. }
  489. }
  490. return false;
  491. }
  492. //Would a solid block at (x, y, z) intersect the player?
  493. intersectsBlock(x, y, z) {
  494. const b = this.box();
  495. return b[0] < x + 1 && b[3] > x && b[1] < y + 1 && b[4] > y && b[2] < z + 1 && b[5] > z;
  496. }
  497. respawn(spawn) {
  498. this.setPos(spawn.x, spawn.y, spawn.z);
  499. this.health = 20;
  500. this.food = 20;
  501. this.saturation = 5;
  502. this.exhaustion = 0;
  503. this.air = 15;
  504. this.dead = false;
  505. this.hurtTime = 0;
  506. this.burnTime = 0;
  507. }
  508. serialize() {
  509. return {
  510. x: this.x, y: this.y, z: this.z, yaw: this.yaw, pitch: this.pitch,
  511. health: this.health, food: this.food, saturation: this.saturation, exhaustion: this.exhaustion,
  512. air: this.air, flying: this.flying
  513. };
  514. }
  515. load(d) {
  516. if (!d) {
  517. return;
  518. }
  519. this.setPos(d.x, d.y, d.z);
  520. this.yaw = d.yaw || 0;
  521. this.pitch = clamp(d.pitch || 0, -1.55, 1.55);
  522. this.health = d.health ?? 20;
  523. this.food = d.food ?? 20;
  524. this.saturation = d.saturation ?? 5;
  525. this.exhaustion = d.exhaustion ?? 0;
  526. this.air = d.air ?? 15;
  527. this.flying = !!d.flying;
  528. }
  529. }
  530. export { gatherBoxes };