Skip to content

Commit ca8a00b

Browse files
committed
Implement memory blocks, messages, and switches
1 parent ff2e98d commit ca8a00b

24 files changed

+937
-251
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mindy-website/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ js-sys = "0.3.77"
1515
mindy = { path = "..", default-features = false, features = ["std", "embedded_graphics", "wasm"] }
1616
wasm-bindgen = "0.2.100"
1717
wee_alloc = "0.4.5"
18+
widestring = "1.2.0"
1819

1920
[dependencies.web-sys]
2021
version = "0.3"

mindy-website/src/buildings.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use std::borrow::Cow;
2+
3+
use mindy::{
4+
types::LAccess,
5+
vm::{Building, CustomBuildingData, InstructionResult, LValue, LogicVM, instructions},
6+
};
7+
use widestring::U16String;
8+
9+
use crate::utils::{on_building_change, u16str_to_js};
10+
11+
pub struct WebMessageData {
12+
value: U16String,
13+
on_building_change: js_sys::Function,
14+
}
15+
16+
impl WebMessageData {
17+
pub fn new(on_building_change: js_sys::Function) -> Self {
18+
Self {
19+
value: U16String::new(),
20+
on_building_change,
21+
}
22+
}
23+
}
24+
25+
impl CustomBuildingData for WebMessageData {
26+
fn read(&mut self, _: &Building, _: &LogicVM, address: Cow<'_, LValue>) -> Option<LValue> {
27+
Some(instructions::Read::read_slice(self.value.as_slice(), &address).into())
28+
}
29+
30+
fn printflush(
31+
&mut self,
32+
building: &Building,
33+
_: &LogicVM,
34+
printbuffer: U16String,
35+
) -> InstructionResult {
36+
self.value = printbuffer;
37+
on_building_change(
38+
&self.on_building_change,
39+
building.position,
40+
"message",
41+
u16str_to_js(&self.value),
42+
);
43+
InstructionResult::Ok
44+
}
45+
46+
fn sensor(&mut self, _: &Building, _: &LogicVM, sensor: LAccess) -> Option<LValue> {
47+
Some(match sensor {
48+
LAccess::BufferSize => self.value.len().into(),
49+
_ => return None,
50+
})
51+
}
52+
}
53+
54+
pub struct WebSwitchData {
55+
enabled: bool,
56+
on_building_change: js_sys::Function,
57+
}
58+
59+
impl WebSwitchData {
60+
pub fn new(on_building_change: js_sys::Function) -> Self {
61+
Self {
62+
enabled: false,
63+
on_building_change,
64+
}
65+
}
66+
}
67+
68+
impl CustomBuildingData for WebSwitchData {
69+
fn control(
70+
&mut self,
71+
building: &Building,
72+
_: &LogicVM,
73+
control: LAccess,
74+
p1: Cow<'_, LValue>,
75+
_: Cow<'_, LValue>,
76+
_: Cow<'_, LValue>,
77+
) -> InstructionResult {
78+
if control == LAccess::Enabled {
79+
self.enabled = p1.bool();
80+
on_building_change(
81+
&self.on_building_change,
82+
building.position,
83+
"switch",
84+
self.enabled,
85+
);
86+
}
87+
InstructionResult::Ok
88+
}
89+
90+
fn sensor(&mut self, _: &Building, _: &LogicVM, sensor: LAccess) -> Option<LValue> {
91+
Some(match sensor {
92+
LAccess::Enabled => self.enabled.into(),
93+
_ => return None,
94+
})
95+
}
96+
}

mindy-website/src/enums.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use mindy::{
2+
types::content,
3+
vm::buildings::{
4+
HYPER_PROCESSOR, LOGIC_PROCESSOR, MEMORY_BANK, MEMORY_CELL, MICRO_PROCESSOR, WORLD_CELL,
5+
WORLD_PROCESSOR,
6+
},
7+
};
8+
use wasm_bindgen::prelude::*;
9+
10+
#[wasm_bindgen]
11+
pub enum DisplayKind {
12+
Logic,
13+
Large,
14+
Tiled,
15+
}
16+
17+
impl DisplayKind {
18+
pub fn name(&self) -> &str {
19+
match self {
20+
Self::Logic => "logic-display",
21+
Self::Large => "large-logic-display",
22+
Self::Tiled => "tile-logic-display",
23+
}
24+
}
25+
}
26+
27+
#[wasm_bindgen]
28+
pub fn display_size(kind: DisplayKind) -> i16 {
29+
content::blocks::FROM_NAME[kind.name()].size
30+
}
31+
32+
#[wasm_bindgen]
33+
pub enum MemoryKind {
34+
Cell,
35+
Bank,
36+
WorldCell,
37+
}
38+
39+
impl MemoryKind {
40+
pub fn name(&self) -> &str {
41+
match self {
42+
Self::Cell => MEMORY_CELL,
43+
Self::Bank => MEMORY_BANK,
44+
Self::WorldCell => WORLD_CELL,
45+
}
46+
}
47+
}
48+
49+
#[wasm_bindgen]
50+
pub fn memory_size(kind: MemoryKind) -> i16 {
51+
content::blocks::FROM_NAME[kind.name()].size
52+
}
53+
54+
#[wasm_bindgen]
55+
pub enum ProcessorKind {
56+
Micro,
57+
Logic,
58+
Hyper,
59+
World,
60+
}
61+
62+
impl ProcessorKind {
63+
pub fn name(&self) -> &str {
64+
match self {
65+
Self::Micro => MICRO_PROCESSOR,
66+
Self::Logic => LOGIC_PROCESSOR,
67+
Self::Hyper => HYPER_PROCESSOR,
68+
Self::World => WORLD_PROCESSOR,
69+
}
70+
}
71+
}
72+
73+
#[wasm_bindgen]
74+
pub fn processor_size(kind: ProcessorKind) -> i16 {
75+
content::blocks::FROM_NAME[kind.name()].size
76+
}

0 commit comments

Comments
 (0)