code wiki / _hdl_build / nx_entity_gate.nx
nx_entity_gate.nx source
↩ module page · 76 lines · 3125 B
1// nx_entity_gate.nx -- start of GAMEPLAY: entities with per-frame state on the sovereign 3D world. 5 creatures
2// wander (position updated each frame, bounce at edges), placed on the terrain, rendered as boxes via the world's
3// object primitive. 4 frames -> 2x2 contact sheet -> sovereign PNG (nx_png). The WORLD is static and the ENTITIES
4// move => proves entity state evolves per frame (not just camera). exit 0 = rendered. license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_voxel_world.nx"
7import "nx_png.nx"
8
9const SW: i64 = 640
10const SH: i64 = 480
11const NE: i64 = 5
12
13func mk(seed: i64, n: i64, water: i64, maxh: i64, latsp: i64,
14 psin: i64, pcos: i64, ysin: i64, ycos: i64, push: i64, focal: i64, style: i64) -> *i64 {
15 let s: *i64 = sys_mmap(12 * 8) as *i64
16 s[0] = seed; s[1] = n; s[2] = water; s[3] = maxh; s[4] = latsp
17 s[5] = psin; s[6] = pcos; s[7] = ysin; s[8] = ycos; s[9] = push; s[10] = focal; s[11] = style
18 return s
19}
20
21func main() -> i64 {
22 let fb: *i64 = sys_mmap(world_fb_bytes()) as *i64
23 let zb: *i64 = sys_mmap(world_fb_bytes()) as *i64
24 let sheet: *i64 = sys_mmap(SW * SH * 8) as *i64
25 var s: i64 = 0
26 while s < SW * SH { sheet[s] = world_sky(); s = s + 1 }
27 let R: *i64 = sys_mmap(128) as *i64
28 let sp: *i64 = mk(7, 14, 1, 6, 4, 574, 819, 574, 819, 24, 300, 0)
29
30 // entity state: position + x-velocity + colour.
31 let ex: *i64 = sys_mmap(NE * 8) as *i64
32 let ez: *i64 = sys_mmap(NE * 8) as *i64
33 let ev: *i64 = sys_mmap(NE * 8) as *i64
34 let ec: *i64 = sys_mmap(NE * 8) as *i64
35 ex[0] = 2; ez[0] = 4; ev[0] = 1; ec[0] = 226 + 70 * 256 + 70 * 65536
36 ex[1] = 10; ez[1] = 6; ev[1] = 0 - 1; ec[1] = 180 + 90 * 256 + 226 * 65536
37 ex[2] = 4; ez[2] = 9; ev[2] = 1; ec[2] = 240 + 170 * 256 + 60 * 65536
38 ex[3] = 9; ez[3] = 11; ev[3] = 0 - 1; ec[3] = 235 + 120 * 256 + 190 * 65536
39 ex[4] = 6; ez[4] = 2; ev[4] = 1; ec[4] = 90 + 210 * 256 + 220 * 65536
40
41 var f: i64 = 0
42 while f < 4 {
43 render_world(sp, fb, zb)
44 build_cam(sp, R)
45 var k: i64 = 0
46 while k < NE {
47 let cy: i64 = world_height(sp, ex[k], ez[k])
48 world_obox(sp, fb, zb, R, ex[k], cy, ez[k], 1, 3, 1, ec[k], 0) // a standing creature (taller = visible)
49 k = k + 1
50 }
51 // composite this frame into the sheet (col=f%2, row=f/2).
52 let qx: i64 = (f % 2) * 320
53 let qy: i64 = (f / 2) * 240
54 var y: i64 = 0
55 while y < 240 {
56 var x: i64 = 0
57 while x < 320 {
58 sheet[(qy + y) * SW + (qx + x)] = fb[y * 320 + x]
59 x = x + 1
60 }
61 y = y + 1
62 }
63 // per-frame UPDATE: move + bounce off the world edges.
64 k = 0
65 while k < NE {
66 var nx: i64 = ex[k] + ev[k]
67 if nx < 1 { nx = 1; ev[k] = 0 - ev[k] }
68 if nx > 12 { nx = 12; ev[k] = 0 - ev[k] }
69 ex[k] = nx
70 k = k + 1
71 }
72 f = f + 1
73 }
74 write_png(sheet, SW, SH, "web_assets/_game_build/world_entities.png" as *u8)
75 return 0
76}