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

Commit ab38e12

Browse files
committed
Growable pixels
1 parent 0512828 commit ab38e12

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

frontend/src/components/PropertyEditor.svelte

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { store } from '../game';
33
44
const propertyDescriptions: Record<number, string> = {
5+
16: 'Grows',
6+
23: 'Causes growth',
57
24: 'Floats',
68
32: 'Enable gravity',
79
};

src/conjam.gleam

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import brush.{apply_brush}
22
import canvas
33
import constants.{max_x, max_y}
44
import gravity.{apply_gravity}
5+
import growth.{apply_growth}
56

67
pub fn draw_frame(
78
frame_number: Int,
@@ -46,6 +47,7 @@ fn iter_pixels(
4647
_, _ -> {
4748
let new_frame_data =
4849
frame_data
50+
|> apply_growth(x, y, next_pixel_offset)
4951
|> apply_gravity(x, y, next_pixel_offset)
5052

5153
iter_pixels(new_frame_data, x + next_pixel_offset, y, next_pixel_offset)

src/growth.gleam

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import canvas
2+
import constants.{max_x, max_y}
3+
import gleam/bool
4+
import properties
5+
6+
pub fn apply_growth(
7+
image_data: canvas.ImageData,
8+
x: Int,
9+
y: Int,
10+
next_pixel_offset: Int,
11+
) -> canvas.ImageData {
12+
use <- canvas.ensure_pixel_exists(image_data, x, y)
13+
14+
let pixel_val = canvas.get_pixel(image_data, x, y)
15+
16+
use <- bool.guard(when: pixel_val == 0x00000000, return: image_data)
17+
use <- bool.guard(
18+
when: !properties.has_property(pixel_val, properties.grows),
19+
return: image_data,
20+
)
21+
22+
let next_x = x + next_pixel_offset
23+
24+
let next_pixel_val = case next_x < 0 || next_x > max_x {
25+
True -> 0x00000000
26+
False -> canvas.get_pixel(image_data, next_x, y)
27+
}
28+
29+
let next_pixel_causes_growth =
30+
properties.has_property(next_pixel_val, properties.causes_growth)
31+
32+
case next_pixel_causes_growth {
33+
True -> {
34+
image_data
35+
|> canvas.set_pixel(next_x, y, pixel_val)
36+
}
37+
False -> {
38+
let above_pixel_val = case y + 1 > max_y {
39+
True -> 0x00000000
40+
False -> canvas.get_pixel(image_data, x, y + 1)
41+
}
42+
43+
let above_pixel_causes_growth =
44+
properties.has_property(above_pixel_val, properties.causes_growth)
45+
46+
case above_pixel_causes_growth {
47+
True -> {
48+
image_data
49+
|> canvas.set_pixel(x, y + 1, pixel_val)
50+
}
51+
False -> {
52+
let below_pixel_val = case y - 1 < 0 {
53+
True -> 0x00000000
54+
False -> canvas.get_pixel(image_data, x, y - 1)
55+
}
56+
57+
let below_pixel_causes_growth =
58+
properties.has_property(below_pixel_val, properties.causes_growth)
59+
60+
case below_pixel_causes_growth {
61+
True -> {
62+
image_data
63+
|> canvas.set_pixel(x, y - 1, pixel_val)
64+
}
65+
False -> image_data
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}

src/properties.gleam

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import gleam/int
22

3+
// 00000000 00000001 00000000 00000000
4+
pub const grows = 0x00010000
5+
6+
// 00000000 00000000 00000010 00000000
7+
pub const causes_growth = 0x00000200
8+
39
// 00000000 00000000 00000001 00000000
410
pub const floats = 0x00000100
511

0 commit comments

Comments
 (0)