Cleaned up some

This commit is contained in:
A Bass 2022-04-30 17:49:20 -04:00
parent 6b9c59920a
commit 95da35ee00
9 changed files with 1064 additions and 317 deletions

View file

@ -4,48 +4,12 @@ const gridSize = [18, 18];
const perlinScale = 3; const perlinScale = 3;
var express = require('express'); var express = require('express');
class Server { process.title = "Server"
constructor(){
this.games = []
}
addGame(name) {
let roomNames = []
for (let i = 0; i < this.games.length; i++) {
roomNames.push(this.games[i].name)
}
if (!roomNames.includes(name)) {
const game = new Game([18,18], name)
this.games.push(game)
console.log("added game - " + game.name)
} else {
console.log("tried to make room already taken")
}
}
getGameByName(name, mode) {
var game = this.games.filter(obj => obj.name == name)[0]
console.log(name, mode, game)
console.log(game == undefined && mode)
if (game == undefined && mode) {
this.addGame(name)
return this.games.filter(obj => obj.name == name)[0]
}
return game;
}
removeGame(name) {
this.players = this.players.filter(obj => obj.name != name);
console.log("removed goom - " + name)
}
}
class Player { class Player {
constructor(id, color) { constructor(id, name, color) {
this.id = id; this.id = id;
this.name = name;
this.color = color; this.color = color;
} }
} }
@ -56,7 +20,7 @@ class Game {
this.world = worldgen.generateWorld(gridSize, perlinScale); this.world = worldgen.generateWorld(gridSize, perlinScale);
} }
addPlayer(id) { addPlayer(id, name) {
var color var color
switch(util.randomNumber(1,4)) { switch(util.randomNumber(1,4)) {
case 1: case 1:
@ -75,7 +39,7 @@ class Game {
color = "yellow"; color = "yellow";
break; break;
} }
const player = new Player(id, color); const player = new Player(id, name, color);
this.players.push(player); this.players.push(player);
} }
@ -111,9 +75,10 @@ io.on('connection', function(client) {
client.broadcast.emit('playerList', game.players) client.broadcast.emit('playerList', game.players)
}) })
client.on('joinGame', function(tank){ client.on('joinGame', function(data){
game.addPlayer(client.id); game.addPlayer(client.id);
console.log(client.id + ' joined the game'); console.log(data)
console.log(`${client.id} joined the game as ${data.name} requesting to join room: ${data.room}`);
client.emit('gameVars', {gridSize: gridSize, world: game.world}) client.emit('gameVars', {gridSize: gridSize, world: game.world})
client.broadcast.emit('playerList', game.players) client.broadcast.emit('playerList', game.players)
client.emit('playerList', game.players) client.emit('playerList', game.players)
@ -125,6 +90,7 @@ io.on('connection', function(client) {
}) })
client.on('clickCanvas', function(data){ client.on('clickCanvas', function(data){
console.log(data)
const xu = data.tilePosition[0] const xu = data.tilePosition[0]
const yu = data.tilePosition[1] const yu = data.tilePosition[1]
game.world[xu][yu].structure = data.structure game.world[xu][yu].structure = data.structure

1201
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -2,16 +2,19 @@
"name": "empires", "name": "empires",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "server.js", "main": "index.js",
"dependencies": {
"dotenv": "^16.0.0",
"express": "^4.17.3",
"socket.io": "^4.4.1"
},
"devDependencies": {},
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": {
"type": "git",
"url": "empires"
},
"author": "", "author": "",
"license": "UNLICENSED" "license": "UNLICENSED",
"dependencies": {
"express": "^4.18.1",
"socket.io": "^4.5.0",
"socket.js": "^0.1.4"
}
} }

View file

@ -5,6 +5,7 @@
* Optimisations by Peter Eastman (peastman@drizzle.stanford.edu). * Optimisations by Peter Eastman (peastman@drizzle.stanford.edu).
* Better rank ordering method by Stefan Gustavson in 2012. * Better rank ordering method by Stefan Gustavson in 2012.
* Converted to Javascript by Joseph Gentle. * Converted to Javascript by Joseph Gentle.
* Converted to NPM Package by Jacob Schneider
* *
* Version 2012-03-09 * Version 2012-03-09
* *
@ -14,8 +15,8 @@
* *
*/ */
(function(global){ module.exports = (function(global){
var module = global.noise = {}; var functions = global.noise = {};
function Grad(x, y, z) { function Grad(x, y, z) {
this.x = x; this.y = y; this.z = z; this.x = x; this.y = y; this.z = z;
@ -52,7 +53,7 @@
// This isn't a very good seeding function, but it works ok. It supports 2^16 // This isn't a very good seeding function, but it works ok. It supports 2^16
// different seed values. Write something better if you need more seeds. // different seed values. Write something better if you need more seeds.
module.seed = function(seed) { functions.seed = function(seed) {
if(seed > 0 && seed < 1) { if(seed > 0 && seed < 1) {
// Scale the seed out // Scale the seed out
seed *= 65536; seed *= 65536;
@ -76,7 +77,7 @@
} }
}; };
module.seed(0); functions.seed(0);
/* /*
for(var i=0; i<256; i++) { for(var i=0; i<256; i++) {
@ -92,7 +93,7 @@
var G3 = 1/6; var G3 = 1/6;
// 2D simplex noise // 2D simplex noise
module.simplex2 = function(xin, yin) { functions.simplex2 = function(xin, yin) {
var n0, n1, n2; // Noise contributions from the three corners var n0, n1, n2; // Noise contributions from the three corners
// Skew the input space to determine which simplex cell we're in // Skew the input space to determine which simplex cell we're in
var s = (xin+yin)*F2; // Hairy factor for 2D var s = (xin+yin)*F2; // Hairy factor for 2D
@ -150,7 +151,7 @@
}; };
// 3D simplex noise // 3D simplex noise
module.simplex3 = function(xin, yin, zin) { functions.simplex3 = function(xin, yin, zin) {
var n0, n1, n2, n3; // Noise contributions from the four corners var n0, n1, n2, n3; // Noise contributions from the four corners
// Skew the input space to determine which simplex cell we're in // Skew the input space to determine which simplex cell we're in
@ -248,7 +249,7 @@
} }
// 2D Perlin Noise // 2D Perlin Noise
module.perlin2 = function(x, y) { functions.perlin2 = function(x, y) {
// Find unit grid cell containing point // Find unit grid cell containing point
var X = Math.floor(x), Y = Math.floor(y); var X = Math.floor(x), Y = Math.floor(y);
// Get relative xy coordinates of point within that cell // Get relative xy coordinates of point within that cell
@ -273,7 +274,7 @@
}; };
// 3D Perlin Noise // 3D Perlin Noise
module.perlin3 = function(x, y, z) { functions.perlin3 = function(x, y, z) {
// Find unit grid cell containing point // Find unit grid cell containing point
var X = Math.floor(x), Y = Math.floor(y), Z = Math.floor(z); var X = Math.floor(x), Y = Math.floor(y), Z = Math.floor(z);
// Get relative xyz coordinates of point within that cell // Get relative xyz coordinates of point within that cell
@ -306,5 +307,5 @@
lerp(n011, n111, u), w), lerp(n011, n111, u), w),
v); v);
}; };
return functions;
})(this); })(this);

1
tt.js Normal file
View file

@ -0,0 +1 @@
process.title = "Server"

View file

@ -1,4 +1,4 @@
var noise = require('noisejs'); var noise = require('./perlin.js');
const util = require("./util.js") const util = require("./util.js")
function generateWorld(gridSize, perlinScale){ function generateWorld(gridSize, perlinScale){

View file

@ -7,10 +7,10 @@
<link rel="stylesheet" href="style.css"> <link rel="stylesheet" href="style.css">
</head> </head>
<body> <body>
<!-- Canvas used to generate individual sprites off one sprite atlas -->
<canvas class="hidden" id="spriteJank" width=24 height=24></canvas>
<div class="container" id="container" style="display: none;"> <div class="container" id="container" style="display: none;">
<h1>No Longer Secret Project</h1> <h1>No Longer Secret Project</h1>
<canvas class="hidden" id="spriteJank" width=24 height=24></canvas>
<div class="wrapper"> <div class="wrapper">
<div class="canvasStack"> <div class="canvasStack">
<canvas oncontextmenu="return false;" class="canvas1" id="canvas" width=0 height=0></canvas> <canvas oncontextmenu="return false;" class="canvas1" id="canvas" width=0 height=0></canvas>
@ -27,9 +27,13 @@
<div class="menu" id="menu"> <div class="menu" id="menu">
<form id="form"> <form id="form">
<input type="text" id="room" value="room"> <input type="text" id="room" value="room">
<input type="text" id="name" value="bob">
<input type="submit" id="join" value="join"> <input type="submit" id="join" value="join">
</form> </form>
</div> </div>
<div class="status">
<footer id="status">Connection Status</footer>
</div>
</body> </body>
<script src='script.js'></script> <script src='script.js'></script>
</html> </html>

View file

@ -1,6 +1,11 @@
// const perlinScale = 3; // const perlinScale = 3;
const IP_ADDRESS = 'http://127.0.0.1:8082/'
var SERVER_CONNECTION = "disconnected"
const tileSize = 24; const tileSize = 24;
var gridSize; const gridSize = [18, 18];
var cash = 99; var cash = 99;
var world var world
var tiles = [] var tiles = []
@ -9,17 +14,26 @@ var canvas = document.getElementById('canvas');
var hud = document.getElementById('hud'); var hud = document.getElementById('hud');
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');
var hctx = hud.getContext('2d'); var hctx = hud.getContext('2d');
var socket = io.connect('http://127.0.0.1:8082/'); var socket = io.connect(IP_ADDRESS);
function joinGame(socket, room, nick){ function joinGame(socket, data){
socket.emit('joinGame', {room, nick}); socket.emit('joinGame', data);
} }
function leaveGame(socket){ function leaveGame(socket){
socket.emit('leaveGame', {}); socket.emit('leaveGame', {});
} }
socket.on('connect', function(data){
SERVER_CONNECTION = "connected"
updateConnectionStatus();
})
socket.on('disconnect', function(data){
SERVER_CONNECTION = "disconnected"
updateConnectionStatus();
})
function updateConnectionStatus() {
document.getElementById("status").textContent = `Server Connection: ${SERVER_CONNECTION}`
}
class Cursor { class Cursor {
constructor(){ constructor(){
@ -40,8 +54,6 @@ window.onbeforeunload = function(){
socket.on('gameVars', function(tank){ socket.on('gameVars', function(tank){
gridSize = tank.gridSize; gridSize = tank.gridSize;
world = tank.world; world = tank.world;
fill_canvas()
}); });
socket.on('playerList', function(data){ socket.on('playerList', function(data){
@ -72,49 +84,47 @@ function loadSprites(){
} }
} }
} }
spriteJank.remove();
} }
function submit(event) { function submit(event) {
document.getElementById('menu').style = "display: none;" document.getElementById('menu').style = "display: none;"
document.getElementById('container').style = "" document.getElementById('container').style = ""
const room = document.getElementById("room").value const room = document.getElementById("room").value
const name = document.getElementById("name").value
if (room == '' || name == '' || SERVER_CONNECTION == 'disconnected') return
joinGame(socket, {room: room, name: name})
event.preventDefault(); event.preventDefault();
joinGame(socket, {room: room, name: "bob"})
} }
window.onload = function () { window.onload = function () {
// Register Events
const form = document.getElementById('form'); const form = document.getElementById('form');
form.addEventListener('submit', submit); form.addEventListener('submit', submit);
loadSprites() hud.addEventListener('mousemove', e => {
mouseMoved(e)
});
hud.addEventListener("mousedown", function(e)
{
getMousePosition(canvas, e);
});
//Set canvases to be ready
canvas.width = tileSize*gridSize[0];
canvas.height = tileSize*gridSize[1];
hud.width = canvas.width
hud.height = canvas.height
//Load all sprites into memory
loadSprites()
} }
function fill_canvas() {
// CREATE CANVAS CONTEXT.
hud.addEventListener('mousemove', e => {
mouseMoved(e)
});
hud.addEventListener("mousedown", function(e)
{
getMousePosition(canvas, e);
});
canvas.width = tileSize*gridSize[0];
canvas.height = tileSize*gridSize[1];
hud.width = canvas.width
hud.height = canvas.height
updateUI();
}
function randomNumber(min, max) { function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min); return Math.floor(Math.random() * (max - min) + min);
} }
@ -170,9 +180,6 @@ function render() { // DRAW THE IMAGE TO THE CANVAS.
} }
console.log("image Rendered") console.log("image Rendered")
} }
function updateUI(){
document.getElementById("cash").innerHTML = ("$" + cash +socket.id);
}
function getMousePosition(canvas, event) { function getMousePosition(canvas, event) {
// Get mouse position on canvas // Get mouse position on canvas

View file

@ -14,6 +14,10 @@ canvas {
/* margin: auto; */ /* margin: auto; */
} }
.status{
color: green;
}
.hidden { .hidden {
display: none; display: none;
} }