From af6952740368a1e2f2b7866e89327e15c3ae0942 Mon Sep 17 00:00:00 2001 From: onezDerv Date: Sun, 29 Sep 2024 01:58:56 -0400 Subject: [PATCH] minor refactoring --- index.html | 7 ++++--- js/index.js | 36 ++---------------------------------- js/player.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 38 deletions(-) diff --git a/index.html b/index.html index a0992bb..5688d67 100644 --- a/index.html +++ b/index.html @@ -9,9 +9,10 @@

UniButton

-

The left mouse is the only button. Click to launch... figure out the rest of the combos yourself.

-

By Onez, more games at Discord

+

The left mouse button is the only input. No keyboard, moving the mouse, or anything else.

+

Click to launch... figure out the rest of the combos yourself.

+

Created by Onez. Join our Discord!

diff --git a/js/index.js b/js/index.js index ae41537..848ba33 100644 --- a/js/index.js +++ b/js/index.js @@ -13,7 +13,7 @@ function Game() { } let canvas = document.querySelector("#canvas"); - let player = new Player(); + let player = new Player(true); let entities = [player]; for (let i = 0; i < 50; i++) { @@ -30,7 +30,6 @@ function Game() { } Game.prototype.main = function () { - let { entities, player } = this; if (player.health <= 0) { @@ -38,38 +37,7 @@ Game.prototype.main = function () { } for (let ent of entities) { - ent.pos.x += ent.vel.x; - ent.pos.y += ent.vel.y; - - ent.vel.x *= 0.9; - ent.vel.y *= 0.9; - - ent.rot += 0.03 * ent.dir; - ent.rot = ent.rot % (Math.PI * 10); - - ent.camera.x = -ent.pos.x * 0.1 + ent.camera.x * 0.9; - ent.camera.y = -ent.pos.y * 0.1 + ent.camera.y * 0.9; - - ent.ticks++; - - let dist = ((ent.pos.x - player.pos.x) ** 2) + ((ent.pos.y - player.pos.y) ** 2); - - let dp = (Math.sin(ent.rot) * (ent.pos.x - player.pos.x)) - - (Math.cos(ent.rot) * (ent.pos.y - player.pos.y)); - - dp /= dist; - - dp *= 10; - - if (ent == player) continue; - - if (Math.random() < -dp && Math.random() > 3 / (ent.ticks+2)) { - ent.bump(); - } - - if (Math.sqrt(dist) < 96 / 2) { - player.health --; - } + ent.handleTick(this); } } diff --git a/js/player.js b/js/player.js index 097a4ae..0396490 100644 --- a/js/player.js +++ b/js/player.js @@ -1,4 +1,4 @@ -function Player() { +function Player(you) { let pos = { x: Math.random() * 5000 - 50, y: Math.random() * 5000 - 50 }; let camera = { x: -pos.x, y: -pos.y }; let vel = { x: 0, y: 0 }; @@ -11,6 +11,8 @@ function Player() { this.ticks = 0; this.health = 100; + + this.you = you; } Player.prototype.bump = function () { @@ -27,4 +29,43 @@ Player.prototype.bump = function () { player.vel.y -= Math.cos(player.rot) * 12; player.ticks = 0; +} + +Player.prototype.handleTick = function(game) { + let { player } = game; + + let ent = this; + + ent.pos.x += ent.vel.x; + ent.pos.y += ent.vel.y; + + ent.vel.x *= 0.9; + ent.vel.y *= 0.9; + + ent.rot += 0.03 * ent.dir; + ent.rot = ent.rot % (Math.PI * 10); + + ent.camera.x = -ent.pos.x * 0.1 + ent.camera.x * 0.9; + ent.camera.y = -ent.pos.y * 0.1 + ent.camera.y * 0.9; + + ent.ticks++; + + let dist = ((ent.pos.x - player.pos.x) ** 2) + ((ent.pos.y - player.pos.y) ** 2); + + let dp = (Math.sin(ent.rot) * (ent.pos.x - player.pos.x)) + - (Math.cos(ent.rot) * (ent.pos.y - player.pos.y)); + + dp /= dist; + + dp *= 10; + + if (ent.you) return; + + if (Math.random() < -dp && Math.random() > 3 / (ent.ticks+2)) { + ent.bump(); + } + + if (Math.sqrt(dist) < 96 / 2) { + player.health --; + } } \ No newline at end of file