Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Final Frontier #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@
<head>
<meta charset="utf-8" />
<title>Ably Arcade</title>

<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>

<script
charset="utf-8"
type="text/javascript"
src="//js.hsforms.net/forms/v2.js"
></script>
<script src="./script.ts" type="module"></script>

<link rel="stylesheet" href="./style.css" />
<link rel="icon" type="image/png" href="assets/favicon.ico">
<link rel="stylesheet" href="./space.css" />
<link rel="icon" type="image/png" href="assets/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

</head>

<body>
<h1>Ably arcade</h1>
<header></header>

<section class="container">
<div id="login">
<div id="signup-form"></div>
</div>

<div id="arcade">
<div class="canvas-holder" id="game-root"></div>
<div id="spectatorControls" class="spectator-controls">
<button class="start" id="start">Start Game</button>
<p class="message" id="message">
3..2..1..GO!
</p>
<p class="message" id="message">3..2..1..GO!</p>
</div>
<div class="controls">
<button id="up" class="dpad" data-keycode="38">Up</button>
Expand All @@ -36,17 +40,21 @@ <h1>Ably arcade</h1>
</div>
</div>
</section>
<div id="spectate">Let me spectate</div>

<canvas id="final-frontier"></canvas>

<footer></footer>

<div id="spectate">Let me spectate</div>

<template id="scoreboard-template">
<section class="scoreboard">
<h2>High Scores</h2>
<table>
<tr>
<th>Name</th>
<th>Score</th>
</tr>
<tr>
<th>Name</th>
<th>Score</th>
</tr>
</table>
</section>
</template>
Expand All @@ -57,5 +65,6 @@ <h2>High Scores</h2>
</tr>
</template>

<script src="./space.js" type="module"></script>
</body>
</html>
37 changes: 37 additions & 0 deletions app/space.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
header {
background-image: url("/assets/topgrid.png");
background-position: top left;
background-repeat: no-repeat;
background-size: cover;
z-index: 1;
left: 0;
right: 0;
top: 0;
height: 26vh;
position: fixed;
}

footer{
position: fixed;
background-image: url("/assets/bottomgrid.png");
background-position: bottom;
background-repeat: no-repeat;
background-size: cover;
z-index: 1;
bottom: 0;
left: 0;
right: 0;
height: 30vh;
}

#final-frontier {
position: fixed;
height: 100%;
width: 100%;
z-index: 0;
}

h1 {
position: relative;
z-index: 3;
}
82 changes: 82 additions & 0 deletions app/space.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const canvas = document.getElementById("final-frontier");
const c = canvas.getContext("2d");

let w, h, notNow;
let universe = celestialBodies(10000);

setCanvasExtents();
requestAnimationFrame(letThereBeLight);
window.onresize = setCanvasExtents;

function letThereBeLight(time) {
notNow = time;
requestAnimationFrame(pulse);
}

function pulse(time) {
const elapsed = time - notNow;
notNow = time;

hyperjump(elapsed);
clear();

const cx = w / 2;
const cy = h / 2;

universe.forEach((star) => {
const x = cx + star.x / (star.z * 0.001);
const y = cy + star.y / (star.z * 0.001);
const offScreen = x < 0 || x >= w || y < 0 || y >= h;

if (offScreen) return;

const distance = star.z / 1000.0;
const z = 1 - distance * distance;

createStar(x, y, z);
});

requestAnimationFrame(pulse);
}

function celestialBodies(count) {
const { random } = Math;
const { clientWidth, clientHeight } = document.body;
return [...Array(count)].map(() => ({
x: random() * clientWidth - clientWidth / 2,
y: random() * clientHeight - clientHeight / 2,
z: random() * 1000,
}));
}

function setCanvasExtents() {
const { clientWidth, clientHeight } = document.body;
w = clientWidth;
h = clientHeight;
canvas.width = clientWidth;
canvas.height = clientHeight;
}

function clear() {
c.fillStyle = "black";
c.fillRect(0, 0, canvas.width, canvas.height);
}

function createStar(x, y, z, px = 4) {
const intensity = 0.6;
const alpha = z * intensity;
const size = px * z;
c.fillStyle = `rgba(255,255,255,${alpha})`;
c.fillRect(x, y, size, size);
}

function hyperjump(epoc) {
const warpspeed = 0.03;
const velocity = epoc * warpspeed;

// update the star position on z axis
universe.forEach((s) => {
s.z -= velocity;
s.z += s.z <= 1 ? 1000 : 0;
});
}