minor refactoring
This commit is contained in:
parent
e87632f1ba
commit
af69527403
3 changed files with 48 additions and 38 deletions
|
@ -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>
|
||||
|
|
36
js/index.js
36
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
43
js/player.js
43
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 () {
|
||||
|
@ -28,3 +30,42 @@ Player.prototype.bump = function () {
|
|||
|
||||
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 --;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue