initial commit.

This commit is contained in:
trans_soup 2023-10-25 19:17:07 +02:00
commit 8c62088bd1
8 changed files with 165 additions and 0 deletions

12
assets/styles/base.css Normal file
View File

@ -0,0 +1,12 @@
html, body {
margin: 0;
min-height: 100vh;
overflow: auto;
}
body {
background-color: #000;
color: #fff;
padding: 0;
}

BIN
favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 B

15
index.html Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" href="./assets/styles/base.css">
<title>precision game</title>
<script defer type="module" src="./js/main.mjs"></script>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
</body>

86
js/entity.mjs Normal file
View File

@ -0,0 +1,86 @@
import { assert } from "./test.mjs";
export function create (x = 0, y = 0, width = 0, height = 0) {
return {
pos: {
x: x,
y: y,
},
size: {
x: width,
y: height,
},
};
}
export function get_pos (entity) {
return entity.pos;
}
export function set_pos (entity, x, y) {
entity.pos.x = x;
entity.pos.y = y;
}
export function get_size (entity) {
return entity.size;
}
export function set_size (entity, x, y) {
entity.size.x = x;
entity.size.y = y;
}
export function move (entity, x, y) {
entity.pos.x += x;
entity.pos.y += y;
}
// TESTS
assert("entity creation works.", _ => {
const e = create(1, 2, 3, 4);
return(
e.pos.x == 1 &&
e.pos.y == 2 &&
e.size.x == 3 &&
e.size.y == 4
);
});
assert("entity `get_pos` works.", _ => {
const e = create(1, 2);
const {x, y} = get_pos(e);
return(x === 1 && y === 2);
});
assert("entity `set_pos` works.", _ => {
const e = create();
set_pos(e, 5, 7);
const {x, y} = get_pos(e);
return(x === 5 && y === 7);
});
assert("entity `get_size` works.", _ => {
const e = create(2, 4, 6, 8);
const {x, y} = get_size(e);
return(x === 6 && y === 8);
});
assert("entity `set_size` works.", _ => {
const e = create(2, 4, 6, 8);
set_size(e, 3, 2);
const {x, y} = get_size(e);
return(x === 3 && y === 2);
});
assert("entity `move` works.", _ => {
const e = create(0, 0);
move(e, 50, 25);
const {x, y} = get_pos(e);
return(x === 50 && y === 25);
});

31
js/graphics.mjs Normal file
View File

@ -0,0 +1,31 @@
import { assert } from "./test.mjs";
import { player } from "./player.mjs";
import { get_pos, get_size } from "./entity.mjs";
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
function clear (color = "#000") {
ctx.fillStyle = color;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function fill_rect (x, y, w, h, color = "#fff") {
ctx.fillStyle = color;
ctx.fillRect(x, y, w, h);
}
function draw_entity (entity, color) {
let pos = get_pos(entity);
let size = get_size(entity);
fill_rect(pos.x, pos.y, size.x, size.y, color);
}
export function render () {
clear();
draw_entity(player, "#0f0");
}

7
js/main.mjs Normal file
View File

@ -0,0 +1,7 @@
import { assert } from "./test.mjs";
import { render } from "./graphics.mjs";
import { set_pos, get_pos, move } from "./entity.mjs";
import { player } from "./player.mjs";
render();

6
js/player.mjs Normal file
View File

@ -0,0 +1,6 @@
import { assert } from "./test.mjs";
export const player = {
pos: { x: 0, y: 0 },
size: { x: 20, y: 20 }
};

8
js/test.mjs Normal file
View File

@ -0,0 +1,8 @@
export function assert (message, check) {
if (typeof check == "function") {
check = check();
}
if (check !== true) {
throw("assertion failure: " + message);
}
}