add emotes and npcs. this really needs a cleanup

This commit is contained in:
biglyderv 2024-10-01 04:03:55 -04:00
parent bd59c3bbe0
commit 3d4f76d6c8
No known key found for this signature in database
GPG key ID: 33AC87E9ACE66954
11 changed files with 267 additions and 85 deletions

30
common/npc.js Normal file
View file

@ -0,0 +1,30 @@
import { distF, uuidv4 } from './util.js'
class NPC {
constructor(you) {
let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 };
let vel = { x: 0, y: 0 };
this.pos = pos;
this.vel = vel;
this.you = you || uuidv4();
this.type = 'NPC';
this.immortal = true;
}
handleTick(game) {
let { entities } = game;
let rEntity = entities[Math.floor(Math.random() * entities.length)];
this.vel.x += (rEntity.pos.x - this.pos.x) * 0.0001;
this.vel.y += (rEntity.pos.y - this.pos.y) * 0.0001;
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
}
}
export default NPC;

View file

@ -1,16 +1,7 @@
import crypto from "../crypto.js";
import { distF, uuidv4 } from './util.js'
function distF(ent, target) {
return ((ent.pos.x - target.pos.x) ** 2) + ((ent.pos.y - target.pos.y) ** 2)
}
function uuidv4() {
return "#000000-000000".replace(/[018]/g, c =>
(+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
);
}
function Player(you, isPlayer) {
class Player {
constructor(you, isPlayer) {
let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 };
let camera = { x: -pos.x, y: -pos.y };
let vel = { x: 0, y: 0 };
@ -29,9 +20,14 @@ function Player(you, isPlayer) {
this.isPlayer = isPlayer;
this.headCount = 0;
}
Player.prototype.bump = function () {
this.type = 'Player';
this.isMenu = false;
this.r = 1;
}
bump() {
let player = this;
if (player.ticks < 7) {
@ -48,8 +44,7 @@ Player.prototype.bump = function () {
player.ticks = 0;
}
Player.prototype.handleTick = function (game) {
handleTick(game) {
let { entities, width, height } = game;
let ent = this;
@ -84,6 +79,11 @@ Player.prototype.handleTick = function (game) {
dp /= Math.sqrt(dist + 0.1);
if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) {
if (target.type == 'NPC') {
ent.isMenu = true;
}
if (target.immortal) continue;
target.health--;
if (target.health == 0) {
@ -92,5 +92,6 @@ Player.prototype.handleTick = function (game) {
}
}
}
}
export default Player;

16
common/util.js Normal file
View file

@ -0,0 +1,16 @@
import crypto from "../crypto.js";
function distF(ent, target) {
return ((ent.pos.x - target.pos.x) ** 2) + ((ent.pos.y - target.pos.y) ** 2)
}
function uuidv4() {
return "#000000-000000".replace(/[018]/g, c =>
(+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
);
}
export {
distF,
uuidv4
}

13
game.js
View file

@ -1,4 +1,5 @@
import GameBasic from "./common/game_basic.js";
import NPC from "./common/npc.js";
class Game extends GameBasic {
constructor() {
@ -9,8 +10,8 @@ class Game extends GameBasic {
let { entities } = this;
let entList = [];
for (let entity of entities) {
let { pos, vel, rot, dir, health, headCount, you, camera, ticks } = entity;
entList.push({ pos, vel, rot, dir, health, headCount, you, camera, ticks });
let { pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing } = entity;
entList.push({ pos, vel, rot, dir, health, headCount, you, camera, ticks, type, isMenu, r, playing });
}
if (entList.length == 0) return;
@ -19,6 +20,10 @@ class Game extends GameBasic {
if (!client.active) continue;
client.send(JSON.stringify(entList));
}
for (let entity of entities) {
entity.r = 1;
}
}
init() {
super.init();
@ -26,6 +31,10 @@ class Game extends GameBasic {
let that = this;
that.entities = [];
for (let i = 0; i < 10; i++) {
that.entities.push(new NPC())
}
setInterval(function () { that.sync() }, 1000 / 5);
}
}

View file

@ -22,8 +22,8 @@ app.ws('/', function (ws, req) {
ws.active = true;
console.log(`A player ${player.you} joined under IP ${req.headers["x-real-ip"]}`)
// This will only work under NGINX.
console.log(`A player ${player.you} joined under IP ${req.headers["x-real-ip"]}`)
ws.on('message', function message(msg) {
let data = {};
@ -33,8 +33,8 @@ app.ws('/', function (ws, req) {
console.log(err);
data = {};
}
let { vel, dir, you, ticks } = data;
let data2 = { vel, dir, you, ticks };
let { vel, dir, you, ticks, isMenu, r, playing } = data;
let data2 = { vel, dir, you, ticks, isMenu, r, playing };
let you2 = game.entities[playerI].you;

Binary file not shown.

BIN
static/assets/No.wav Normal file

Binary file not shown.

BIN
static/assets/Troll.wav Normal file

Binary file not shown.

BIN
static/assets/Yes.wav Normal file

Binary file not shown.

67
static/assets/npc.svg Normal file
View file

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="110"
height="110"
viewBox="0 0 110 110"
version="1.1"
id="svg1"
inkscape:version="1.3.2 (091e20ef0f, 2023-11-25, custom)"
sodipodi:docname="npc.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview1"
pagecolor="#000000"
bordercolor="#000000"
borderopacity="0.25"
inkscape:showpageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d1d1d1"
inkscape:document-units="px"
inkscape:zoom="4.1121712"
inkscape:cx="61.403085"
inkscape:cy="49.608829"
inkscape:window-width="1860"
inkscape:window-height="1004"
inkscape:window-x="30"
inkscape:window-y="46"
inkscape:window-maximized="1"
inkscape:current-layer="layer1"
showgrid="false" />
<defs
id="defs1" />
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(5,5)">
<circle
style="fill:#508341;fill-opacity:1;stroke:#fcf9f9;stroke-width:10;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers"
id="path1"
cx="50"
cy="50"
r="50" />
<path
id="circle4"
style="fill:#35582b;stroke-width:10;stroke-linecap:round;stroke-linejoin:round;paint-order:stroke fill markers"
d="M 50 0 A 50 50 0 0 0 4.5566406 29.15625 C 2.0495143 38.07249 0.46754838 45.280484 0.095703125 48.669922 A 50 50 0 0 0 0 50 C 0 59.3151 34.053758 47.505859 61.667969 47.505859 C 89.282178 47.505859 93.717713 56.877787 100 50 A 50 50 0 0 0 50 0 z " />
<path
style="fill:#fcf9f9;fill-opacity:1;stroke:#fcf9f9;stroke-width:5;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers"
d="M 44,11.529728 V 88.470272"
id="path2" />
<path
style="fill:#fcf9f9;fill-opacity:1;stroke:#fcf9f9;stroke-width:5;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers"
d="M 56,11.529728 V 88.470272"
id="path3" />
<path
style="fill:none;fill-opacity:1;stroke:#fcf9f9;stroke-width:5;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers"
d="M 66.225624,23.599481 C 42.814,24.14986 33.037139,27.938889 30.631342,44.149849 c -2.405797,16.21096 33.147924,6.497412 38.240878,10.246612 5.092954,3.7492 -23.18558,23.936516 -37.818455,21.834495"
id="path4"
sodipodi:nodetypes="czzc" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.8 KiB

View file

@ -1,13 +1,26 @@
import Player from "./player.js";
import GameBasic from "./game_basic.js";
import NPC from "./npc.js";
const cs = 1024;
const assets = [
'assets/player.svg',
'assets/head.svg',
'assets/map.svg'
'assets/map.svg',
'assets/npc.svg'
];
const origin = 'https://ub.zenoverse.net/'
const legalTypes = {
Player,
NPC
}
const options = [
'Troll',
'Exit',
'Elec Piano Loop',
'No',
'Yes'
]
const origin = (window.location.href.indexOf('localhost') != -1) ? window.location.href : 'https://ub.zenoverse.net/'
class Game extends GameBasic {
constructor() {
@ -42,6 +55,7 @@ class Game extends GameBasic {
for (let ent of entities) {
if (ent.health <= 0) continue;
if (ent.type == 'Player') {
ctx.save();
ctx.translate(ent.pos.x, ent.pos.y);
@ -50,12 +64,24 @@ class Game extends GameBasic {
ctx.restore();
if (ent.playing) {
ctx.strokeStyle = 'white';
ctx.lineWidth = "20";
ctx.beginPath();
ctx.arc(ent.pos.x, ent.pos.y, 32, 0, 2 * Math.PI);
ctx.stroke();
}
ctx.fillStyle = ent.you.split('-')[0];
ctx.beginPath();
ctx.arc(ent.pos.x, ent.pos.y, 32, 0, 2 * Math.PI);
ctx.fill();
ctx.drawImage(assetsIn[0], ent.pos.x - 64 / 2, ent.pos.y - 64 / 2, 64, 64);
}
ctx.drawImage((ent.type == 'NPC') ? assetsIn[3] : assetsIn[0], ent.pos.x - 64 / 2, ent.pos.y - 64 / 2, 64, 64);
if (ent.type != 'Player') continue;
ctx.strokeStyle = "rgb(255,255,255)";
ctx.lineWidth = "8";
@ -71,14 +97,20 @@ class Game extends GameBasic {
ctx.restore();
if (player.health <= 0) {
if (player.health <= 0 || player.isMenu) {
ctx.fillStyle = 'rgba(0,0,0,0.5)';
ctx.fillRect(0, 0, cs, cs);
ctx.fillStyle = 'rgb(255,255,255)';
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "bold 48px sans-serif";
ctx.fillText('You died! Click to respawn', cs / 2, cs / 2);
if (player.health <= 0) ctx.fillText('You died! Click to respawn', cs / 2, cs / 2);
if (player.isMenu) {
let r = Math.floor(Math.abs(player.rot / 1.2) % options.length);
ctx.fillText(`Click to react ${options[r]}`, cs / 2, cs / 2)
ctx.font = "bold 16px sans-serif";
ctx.fillText(`Wait for options ${options.join(', ')}`, cs / 2, cs / 2 + 50)
}
}
}
click() {
@ -88,6 +120,10 @@ class Game extends GameBasic {
this.ws = new WebSocket(origin);
this.player = new Player(false, true);
this.entities.push(this.player);
} else if (player.isMenu) {
player.r = Math.floor(Math.abs(player.rot / 1.2) % options.length);
player.isMenu = false;
this.sync();
} else {
player.bump();
this.sync();
@ -96,17 +132,34 @@ class Game extends GameBasic {
sync() {
let { player } = this;
let { vel, dir, you, ticks } = player;
let { vel, dir, you, ticks, isMenu, r, playing } = player;
this.ws.send(JSON.stringify({ vel, dir, you, ticks }));
this.ws.send(JSON.stringify({ vel, dir, you, ticks, isMenu, r, playing }));
}
recv({ data }) {
let { player } = this;
let you = player.you;
let that = this;
let entList = JSON.parse(data);
entList = entList.map(x => {
x.handleTick = Player.prototype.handleTick;
let type = (Object.keys(legalTypes).indexOf(x.type) == -1) ? Player : legalTypes[x.type];
x.handleTick = type.prototype.handleTick;
if (x.r != 1) {
let a = new Audio(`assets/${options[x.r]}.wav`);
a.addEventListener('ended', () => {
if (x.you == you) {
that.player.playing = false;
that.sync();
}
a.remove();
})
if (you != x.you) {
x.r = 1;
}
a.play();
}
return x;
})
@ -114,6 +167,12 @@ class Game extends GameBasic {
this.player = Object.assign(this.player || new Player(false, false), matchingPlayer[0]);
if (this.player.r != 1) {
this.player.playing = true;
this.player.r = 1;
that.sync();
}
this.entities = entList;
if (this.entities.length == 0) this.entities = [this.player];