-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProceduralGeneration.rs
More file actions
73 lines (56 loc) · 2.13 KB
/
ProceduralGeneration.rs
File metadata and controls
73 lines (56 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use consolegameengine::{ Colour, ConsoleGameEngine, Pixel, Rcode };
fn lehmer(seed: u32) -> u32 {
let generator: u64 = seed as u64 + 0xE120FC15;
let mut tmp = generator * 0x4A39B70D;
let m: u32 = ((tmp >> 32) ^ tmp) as u32;
tmp = m as u64 * 0x12FAD5C9;
return ((tmp >> 32) ^ tmp) as u32;
}
fn wrap<T>(v: T, min: T, max: T) -> T
where T: std::ops::Sub<Output = T> + std::ops::Add<Output = T> + std::ops::Rem<Output = T> + Copy {
return v % (max - min) + min;
}
fn main() {
let mut app = ConsoleGameEngine::new();
let mut pan_started = false;
let mut pan_start_x = 0;
let mut pan_start_y = 0;
let mut offset_x = 0;
let mut offset_y = 0;
if let Rcode::Ok = app.construct_console(256, 240, 4, 4, "Procedural Generation") {
while let Rcode::Ok = app.update() {
let mouse_x = app.get_mouse_x();
let mouse_y = app.get_mouse_y();
if app.get_mouse(0).pressed {
pan_start_x = mouse_x;
pan_start_y = mouse_y;
pan_started = true;
}
if app.get_mouse(0).released {
pan_started = false;
}
if pan_started {
offset_x += pan_start_x - mouse_x;
offset_y += pan_start_y - mouse_y;
pan_start_x = mouse_x;
pan_start_y = mouse_y;
}
app.clear(Pixel::Solid as u16, Colour::Black as u16);
app.draw_string(1, 1, format!("({mouse_x}, {mouse_y})"), Colour::FgRed as u16);
for i in 0..app.screen_width() as i32 {
for j in 0..app.screen_height() as i32 {
let seed = (j + offset_y) << 16 | (i + offset_x);
let rand = lehmer(seed as u32);
if rand % 2048 < 4 {
app.fill_circle(
i, j,
wrap(rand as i32, 3, 8),
Pixel::Solid as u16,
wrap(rand, 1, 15) as u16
);
}
}
}
}
}
}