-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsand.spd
More file actions
executable file
·137 lines (112 loc) · 2.86 KB
/
sand.spd
File metadata and controls
executable file
·137 lines (112 loc) · 2.86 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/local/bin/spd
import "GUI"
main
return SandSimulator().RunAsMain(60)
struct Particle
|ParticleType| Type
|int8| VX
|int8| VY
|uint| Start
function Color (|vec4|)
return .type.color
syntax is (|ParticleType| T, assigns:|bool|)
if Value
.type = T
syntax is (|ParticleType| T, |bool|)
return .type == T
function Swap (|&particle| P)
|particle| T
T = P[0]
P[0] = self[0]
self[0] = (&T)[0]
function MoveTo (|?&particle| P, |bool|)
if p and (p.type == 0 or p.type >= ParticleType.Max)
P[0] = self[0]
.Type = 0
return true
function IsLiquid (|bool|)
nil checker
return self!=nil and (.type >= ParticleType.water and .type <= ParticleType.Acid)
function HasGravity (|bool|)
nil checker
return self!=nil and (self isnt wood and stone)
datatype ParticleType (byte)
constants: Empty, Sand, Stone, Water, Lava, Acid, Air, Wood, Max
syntax is (|ParticleType| T, |bool|)
return self == T
function Color (|vec4|)
if self is sand: return vec4(0.5, 0.35, 0.1, 1.0)
if self is stone: return vec4(0.5, 1.0)
if self is Water: return vec4(0.1, 0.4, 1.0, 1.0)
if self is air: return vec4(0.25, 1.0, 1.0, 0.4)
if self is Lava: return vec4(1.0, 0.5, 0.2, 0.5)
if self is wood: return colors.brown
if self is acid: return colors.Green
class SandSimulator (Window)
|GridSection of particle| Particles
constructor
.Particles = GridSection(400,300)|gridsection of particle|//#quit
.reset
function Reset
|| h = Random.int
for s in .Particles
h = h.hash
s[0] = nil
s.Type = h
|| h2 = h&255
(s is water) = h2 > 200
(s is sand) = h2 > 240
function DoGravity (|!&particle| P, |ivec2| O, |&particle|)
|| V = (p.vx, p.vy)
// if o.y == 0
// return p
|| range = v.RayCast()
for CC in range
|| D = .Particles[O+CC]
if p.MoveTo(D)
rz = D
p = D
elseif D.isliquid // adding this causes infinite rain. no idea why.
if !p.IsLiquid
D.swap(p)
D.vy = D.vy|int| max -3
// else
// p2.vy = p.vy
// p2.vx = p.vx
// p.vx = 0
// p.vy = 0
// exit
behaviour FrameStep
require GUIActive // and (.FrameCount+1) isa 16
.Touch
|| rnd = 64K()
for (s in .Particles) (V)
if s.HasGravity
s.vy = (s.vy - 1) max -5
|| D = .DoGravity(S, V)
if !D
s.vy = 1-s.vy
rnd = rnd.hash
|| R = (Rnd&2) - 1
if s.MoveTo(.Particles[V-(R,1)])
0
elseif s.MoveTo(.Particles[V-(-R,1)])
0
elseif s.Isliquid
if s.MoveTo(.Particles[V-(R,0)])
0
elseif s.MoveTo(.Particles[V-(-R,0)])
0
behaviour keydown
if key is space
.reset
return true
behaviour Drawbackground
where.DrawRect(Self, colors.black)
|| w = .Particles.width
|| ws = (where.Width-(w*2))/2
|| base = 40
for (s in .Particles) (V)
if s
|| R = V*2 + (ws,base)
where.DrawRect((R, R+2), s.color)