player.js 19 KB

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