minor refactoring

This commit is contained in:
biglyderv 2024-09-29 01:58:56 -04:00
parent e87632f1ba
commit af69527403
No known key found for this signature in database
GPG key ID: 33AC87E9ACE66954
3 changed files with 48 additions and 38 deletions

View file

@ -9,9 +9,10 @@
<body>
<section id='main'>
<h1>UniButton</h1>
<p>The <b>left mouse</b> is the only button. Click to launch... figure out the rest of the combos yourself.</p>
<p><i>By <a href='https://zenoverse.net/'>Onez</a>, more games at <a
href='https://discord.gg/EpsRZrHswBu'>Discord</a></i></p>
<p>The <b>left mouse button</b> is the only input. No keyboard, moving the mouse, or anything else.</p>
<p>Click to launch... figure out the rest of the combos yourself.</p>
<p><i>Created by <a href='https://zenoverse.net/'>Onez</a>. Join our <a
href='https://discord.gg/EpsRZrHswBu'>Discord</a>!</i></p>
<div>
<canvas id='canvas'></canvas>
</div>

View file

@ -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);
}
}

View file

@ -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 --;
}
}