Skip to content
This repository was archived by the owner on Aug 24, 2024. It is now read-only.

Commit 0297449

Browse files
committed
Element selector
1 parent 26ec843 commit 0297449

File tree

4 files changed

+66
-11
lines changed

4 files changed

+66
-11
lines changed

frontend/src/App.svelte

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import { onMount } from 'svelte';
33
import Controls from './components/Controls.svelte';
4+
import ElementSelector from './components/ElementSelector.svelte';
45
import Framerate from './components/Framerate.svelte';
56
import { init } from './game';
67
@@ -9,14 +10,18 @@
910
});
1011
</script>
1112

12-
<div class="flex w-[640px] flex-col">
13-
<canvas
14-
class="scale z-10 h-[480px] w-full bg-black"
15-
style="image-rendering: pixelated"
16-
id="canvas"
17-
width="640"
18-
height="480"
19-
></canvas>
20-
<Controls />
13+
<div class="flex w-full flex-row">
14+
<div class="flex-1"></div>
15+
<div class="w-[640px]">
16+
<canvas
17+
class="scale relative z-10 h-[480px] w-full bg-black"
18+
style="image-rendering: pixelated"
19+
id="canvas"
20+
width="640"
21+
height="480"
22+
></canvas>
23+
<Controls />
24+
</div>
25+
<div class="h-[480px] flex-1"><ElementSelector /></div>
2126
</div>
2227
<Framerate />

frontend/src/components/Controls.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
{#if controlsDiv}
5454
<button
55-
class="flex items-center justify-between rounded-b-lg bg-zinc-950 p-2 text-xl font-semibold text-teal-700 transition-all hover:bg-teal-700 hover:text-white"
55+
class="flex w-full items-center justify-between rounded-b-lg bg-zinc-950 p-2 text-xl font-semibold text-teal-700 transition-all hover:bg-teal-700 hover:text-white"
5656
style:transform={showControls ? 'none' : `translateY(-${controlsDiv?.clientHeight || '300'}px)`}
5757
on:click={() => {
5858
showControls = !showControls;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<script lang="ts">
2+
import { twMerge } from 'tailwind-merge';
3+
import { store } from '../game';
4+
5+
const selectableElements = {
6+
Eraser: 0x00000000,
7+
Sand: 0xffd880ff,
8+
Water: 0x0000ffff,
9+
Plant: 0x00ff00ff,
10+
Wall: 0x808080fe,
11+
};
12+
13+
function toHex(value: number): string {
14+
return value.toString(16).padStart(8, '0');
15+
}
16+
17+
function toBinary(value: number): string {
18+
return value.toString(2).padStart(32, '0');
19+
}
20+
21+
function computeTextColour(value: number): string {
22+
const r = (value & 0xff000000) >> 24;
23+
const g = (value & 0x00ff0000) >> 16;
24+
const b = (value & 0x0000ff00) >> 8;
25+
26+
// https://graphicdesign.stackexchange.com/a/17562
27+
const gamma = 2.2;
28+
return 0.2126 * Math.pow(r, gamma) + 0.7152 * Math.pow(g, gamma) + 0.0722 * Math.pow(b, gamma) > 0.5
29+
? 'black'
30+
: 'white';
31+
}
32+
</script>
33+
34+
<div class="h-full rounded-r-lg bg-zinc-950">
35+
{#each Object.entries(selectableElements) as [name, value]}
36+
<button
37+
class={twMerge(
38+
'w-full p-2 text-xl font-semibold transition-all hover:opacity-75',
39+
$store.brushColour === toBinary(value) && 'font-black',
40+
)}
41+
style:background-color={'#' + toHex(value)}
42+
style:color={computeTextColour(value)}
43+
on:click={() => {
44+
$store.brushColour = toBinary(value);
45+
}}
46+
>
47+
{name}
48+
</button>
49+
{/each}
50+
</div>

frontend/src/game.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let state = {
1313
mouseY: 0,
1414

1515
brushSize: 10,
16-
brushColour: '11111111000000001111111111111111',
16+
brushColour: '11111111110110001000000011111111',
1717

1818
autoTick: true,
1919
};

0 commit comments

Comments
 (0)