Initial commit

This commit is contained in:
Zakary Timson 2023-01-29 08:18:58 -05:00
commit 7eb019c780
17 changed files with 16793 additions and 0 deletions

8
Cell.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

0
assets/styles/main.scss Normal file
View File

27
index.html Normal file
View File

@ -0,0 +1,27 @@
<!doctype html>
<html lang="en">
<head>
<title>Cells</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Cells">
<style>
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
<script src="node_modules/p5/lib/p5.min.js"></script>
<script src="src/app.ts"></script>
</head>
<body>
<div style="position: absolute; top: 0; left: 0; width: 20%; height: 20%;"></div>
</body>
</html>

16454
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

18
package.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "cell-client",
"version": "0.0.0",
"description": "",
"main": "index.html",
"scripts": {
"start": "parcel index.html --open",
"build": "parcel build index.html"
},
"dependencies": {
"@types/p5": "^1.3.3",
"p5": "^1.4.0"
},
"devDependencies": {
"parcel-bundler": "^1.12.5",
"typescript": "^4.5.4"
}
}

33
src/app.js Normal file
View File

@ -0,0 +1,33 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const p5_1 = __importDefault(require("p5"));
require("/assets/styles/main.scss");
const cell_1 = require("./cell");
const environment_1 = require("./environment");
new p5_1.default((p5) => {
const position = [0, 0];
const player = new cell_1.Cell([0, 0], '');
const environment = new environment_1.Environment([player]);
let drag;
p5.setup = () => {
p5.createCanvas(document.body.offsetWidth, document.body.offsetHeight);
};
p5.draw = () => {
environment.draw(p5, position[0], position[1]);
};
p5.windowResized = () => { p5.resizeCanvas(p5.windowWidth, p5.windowHeight); };
p5.mousePressed = () => { drag = [p5.mouseX, p5.mouseY]; };
p5.mouseReleased = () => { drag = null; };
p5.mouseDragged = () => {
if (drag) {
position[0] += drag[0] - p5.mouseX;
drag[0] = p5.mouseX;
position[1] -= drag[1] - p5.mouseY;
drag[1] = p5.mouseY;
}
};
});
//# sourceMappingURL=app.js.map

1
src/app.js.map Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"app.js","sourceRoot":"","sources":["app.ts"],"names":[],"mappings":";;;;;AAAA,4CAAoB;AACpB,oCAAkC;AAClC,iCAA4B;AAC5B,+CAA0C;AAE1C,IAAI,YAAE,CAAC,CAAC,EAAM,EAAE,EAAE;IACjB,MAAM,QAAQ,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACxB,MAAM,MAAM,GAAG,IAAI,WAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACpC,MAAM,WAAW,GAAG,IAAI,yBAAW,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;IAE9C,IAAI,IAA6B,CAAC;IAElC,EAAE,CAAC,KAAK,GAAG,GAAG,EAAE;QACf,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,EAAE,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACxE,CAAC,CAAA;IAED,EAAE,CAAC,IAAI,GAAG,GAAG,EAAE;QACd,WAAW,CAAC,IAAI,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;IAChD,CAAC,CAAC;IAEF,EAAE,CAAC,aAAa,GAAG,GAAG,EAAE,GAAG,EAAE,CAAC,YAAY,CAAC,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAA;IAE9E,EAAE,CAAC,YAAY,GAAG,GAAG,EAAE,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;IAC1D,EAAE,CAAC,aAAa,GAAG,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;IACzC,EAAE,CAAC,YAAY,GAAG,GAAG,EAAE;QACtB,IAAG,IAAI,EAAE;YACR,QAAQ,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC;YACnC,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC;YACpB,QAAQ,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC;YACnC,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC;SACpB;IACF,CAAC,CAAA;AACF,CAAC,CAAC,CAAC"}

33
src/app.ts Normal file
View File

@ -0,0 +1,33 @@
import P5 from "p5";
import "/assets/styles/main.scss";
import {Cell} from './cell';
import {Environment} from './environment';
new P5((p5: P5) => {
const position = [0, 0];
const player = new Cell([0, 0], '');
const environment = new Environment([player]);
let drag: [number, number] | null;
p5.setup = () => {
p5.createCanvas(document.body.offsetWidth, document.body.offsetHeight);
}
p5.draw = () => {
environment.draw(p5, position[0], position[1]);
};
p5.windowResized = () => { p5.resizeCanvas(p5.windowWidth, p5.windowHeight); }
p5.mousePressed = () => { drag = [p5.mouseX, p5.mouseY]; }
p5.mouseReleased = () => { drag = null; }
p5.mouseDragged = () => {
if(drag) {
position[0] += drag[0] - p5.mouseX;
drag[0] = p5.mouseX;
position[1] -= drag[1] - p5.mouseY;
drag[1] = p5.mouseY;
}
}
});

46
src/cell.js Normal file
View File

@ -0,0 +1,46 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cell = void 0;
const MAX_CELL_SIZE = 100;
class Cell {
constructor(pos, script = '') {
this.pos = pos;
this.script = script;
this.energy = 100;
this.cellWall = 5;
this.api = {
move: {
down: () => this.move(0, -1),
left: () => this.move(-1, 0),
right: () => this.move(1, 0),
up: () => this.move(0, 1)
}
};
}
get dead() { return this.energy <= 20; }
get size() { return Math.min(MAX_CELL_SIZE, 50 * (this.energy / 100)); }
draw(p5, offsetX, offsetY) {
if (!this.dead)
this.run();
p5.fill(100, 100, 200);
p5.stroke(150, 150, 255);
p5.strokeWeight(this.cellWall);
p5.circle(offsetX + this.pos[0], offsetY - this.pos[1], this.size);
}
move(x, y) {
const energyCost = Math.round(this.size / 25 * 10) / 10;
if (this.energy - energyCost <= 20)
return;
this.energy -= energyCost;
this.pos[0] += x;
this.pos[1] += y;
}
run() {
if (!this.script)
return;
const script = `${this.script}; run(this);`;
return function () { return eval(script); }.call(this.api);
}
}
exports.Cell = Cell;
//# sourceMappingURL=cell.js.map

1
src/cell.js.map Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"cell.js","sourceRoot":"","sources":["cell.ts"],"names":[],"mappings":";;;AAWA,MAAM,aAAa,GAAG,GAAG,CAAC;AAE1B,MAAa,IAAI;IAOhB,YAAoB,GAAqB,EAAU,SAAiB,EAAE;QAAlD,QAAG,GAAH,GAAG,CAAkB;QAAU,WAAM,GAAN,MAAM,CAAa;QANtE,WAAM,GAAW,GAAG,CAAC;QACrB,aAAQ,GAAW,CAAC,CAAC;QAOrB,QAAG,GAAY;YACd,IAAI,EAAE;gBACL,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;gBAC5B,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;gBAC5B,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;gBAC5B,E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IAEF,GAAG;QACF,IAAG,CAAC,IAAI,CAAC,MAAM;YAAE,OAAO;QACxB,MAAM,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,cAAc,CAAC;QAC5C,OAAO,cAAa,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3D,CAAC;CACD;AAvCD,oBAuCC"}

53
src/cell.ts Normal file
View File

@ -0,0 +1,53 @@
import P5 from "p5";
export interface CellApi{
move: {
down: () => void;
left: () => void;
right: () => void;
up: () => void;
}
}
const MAX_CELL_SIZE = 100;
export class Cell {
energy: number = 100;
cellWall: number = 5;
get dead() { return this.energy <= 20; }
get size() { return Math.min(MAX_CELL_SIZE, 50 * (this.energy / 100)) }
constructor(private pos: [number, number], private script: string = '') { }
api: CellApi = {
move: {
down: () => this.move(0, -1),
left: () => this.move(-1, 0),
right: () => this.move(1, 0),
up: () => this.move(0, 1)
}
}
draw(p5: P5, offsetX: number, offsetY: number) {
if(!this.dead) this.run();
p5.fill(100, 100, 200);
p5.stroke(150, 150, 255);
p5.strokeWeight(this.cellWall);
p5.circle(offsetX + this.pos[0], offsetY - this.pos[1], this.size);
}
move(x: number, y: number) {
const energyCost = Math.round(this.size / 25 * 10) / 10;
if(this.energy - energyCost <= 20) return;
this.energy -= energyCost;
this.pos[0] += x;
this.pos[1] += y;
}
run() {
if(!this.script) return;
const script = `${this.script}; run(this);`;
return function() { return eval(script); }.call(this.api);
}
}

47
src/environment.js Normal file
View File

@ -0,0 +1,47 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Environment = exports.mulberry32 = void 0;
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
exports.mulberry32 = mulberry32;
const RENDER_DIST = 500;
class Environment {
constructor(cells = [], size = 1000, seed = Math.random()) {
this.cells = cells;
this.size = size;
this.seed = seed;
this.food = [];
this.generator = mulberry32(seed);
this.food = Array(500).fill([]).map(() => [
this.generator() * this.size * (this.generator() > 0.5 ? 1 : -1),
this.generator() * this.size * (this.generator() > 0.5 ? 1 : -1)
]);
}
addCell(cell) {
this.cells.push(cell);
cell.environment = this;
}
draw(p5, x, y) {
const center = [p5.windowWidth / 2, p5.windowHeight / 2];
const offset = [center[0] - x, center[1] + y];
// Background
p5.background(245, 245, 255);
// Food
this.food.filter(f => (Math.abs((f[0] - x) / (f[1] - y)) <= RENDER_DIST)).forEach(f => {
p5.fill(100);
p5.stroke(10);
p5.strokeWeight(2);
p5.circle(offset[0] + f[0], offset[1] - f[1], 10);
});
// Cells
this.cells.forEach(c => c.draw(p5, offset[0], offset[1]));
}
}
exports.Environment = Environment;
//# sourceMappingURL=environment.js.map

1
src/environment.js.map Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"environment.js","sourceRoot":"","sources":["environment.ts"],"names":[],"mappings":";;;AAGA,SAAgB,UAAU,CAAC,IAAY;IACtC,OAAO;QACN,IAAI,CAAC,GAAG,IAAI,IAAI,UAAU,CAAC;QAC3B,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACnC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;QACxC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,UAAU,CAAC;IAC5C,CAAC,CAAA;AACF,CAAC;AAPD,gCAOC;AAED,MAAM,WAAW,GAAG,GAAG,CAAC;AAExB,MAAa,WAAW;IAIvB,YAA6B,QAAgB,EAAE,EAC5B,OAAe,IAAI,EAClB,OAAe,IAAI,CAA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AAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;YACrF,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACb,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YACd,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;YACnB,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACnD,CAAC,CAAC,CAAC;QAEH,QAAQ;QACR,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3D,CAAC;CACD;AAtCD,kCAsCC"}

53
src/environment.ts Normal file
View File

@ -0,0 +1,53 @@
import P5 from 'p5';
import {Cell} from './cell';
export function mulberry32(seed: number) {
return function() {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}
const RENDER_DIST = 500;
export class Environment {
private generator!: () => number;
private food: [number, number][] = [];
constructor(private readonly cells: Cell[] = [],
public readonly size: number = 1000,
private readonly seed: number = Math.random()
) {
this.generator = mulberry32(seed);
this.food = Array(500).fill([]).map(() => [
this.generator() * this.size * (this.generator() > 0.5 ? 1 : -1),
this.generator() * this.size * (this.generator() > 0.5 ? 1 : -1)
]);
}
addCell(cell: Cell) {
this.cells.push(cell);
cell.environment = this;
}
draw(p5: P5, x: number, y: number) {
const center = [p5.windowWidth / 2, p5.windowHeight / 2];
const offset = [center[0] - x, center[1] + y];
// Background
p5.background(245, 245, 255);
// Food
this.food.filter(f => (Math.abs((f[0] - x) / (f[1] - y)) <= RENDER_DIST)).forEach(f => {
p5.fill(100);
p5.stroke(10);
p5.strokeWeight(2);
p5.circle(offset[0] + f[0], offset[1] - f[1], 10);
});
// Cells
this.cells.forEach(c => c.draw(p5, offset[0], offset[1]));
}
}

3
src/example.js Normal file
View File

@ -0,0 +1,3 @@
const run = (cell) => {
cell.move.up();
};

1
src/example.js.map Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"example.js","sourceRoot":"","sources":["example.ts"],"names":[],"mappings":";;AAEA,IAAM,GAAG,GAAG,UAAC,IAAa;IACzB,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC;AAChB,CAAC,CAAC"}

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"jsx": "preserve",
"allowJs": true,
"rootDir": "src",
"esModuleInterop": true,
"lib": ["DOM", "ESNext"],
"module": "commonjs",
"moduleResolution": "Node",
"sourceMap": true,
"target": "ES2016",
"strict": true,
}
}