Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | rem https://forum.thegamecreators.com/thread/160210#msg1886513 |
2 | |||
3 | // Screen settings |
||
4 | sync on |
||
5 | sync rate 60 |
||
6 | autocam off |
||
7 | |||
8 | #constant SPREAD1_MAX 10 |
||
9 | #constant SPREAD2_MAX 15 |
||
10 | |||
11 | gosub _setup_particles |
||
12 | |||
13 | // Main loop |
||
14 | do |
||
15 | // Check when SPACE is pressed |
||
16 | if spacekey() = 1 and keySP = 0 |
||
17 | gosub _splat_blood |
||
18 | endif |
||
19 | keySP = spacekey() |
||
20 | |||
21 | gosub _handle_blood |
||
22 | loop |
||
23 | |||
24 | // --------------------- |
||
25 | |||
26 | _setup_particles: |
||
27 | // Make 100 particle objects |
||
28 | particleIndex = 0 |
||
29 | #constant PARTICLEMAX = 100 |
||
30 | dim particle(PARTICLEMAX) as particleType |
||
31 | for i = 1 to PARTICLEMAX |
||
32 | make object plain i, 2, 2 |
||
33 | color object i, rgb(128, 0, 0) |
||
34 | exclude object on i |
||
35 | next i |
||
36 | return |
||
37 | |||
38 | _handle_blood: |
||
39 | // Update particles |
||
40 | for i = 1 to PARTICLEMAX |
||
41 | UpdateParticle(i) |
||
42 | next i |
||
43 | |||
44 | // Update screen |
||
45 | sync |
||
46 | return |
||
47 | |||
48 | _splat_blood: |
||
49 | yoff = rnd(SPREAD1_MAX*2)-SPREAD1_MAX; |
||
50 | |||
51 | // Activate blood particles around box |
||
52 | for i = 1 to PARTICLEMAX |
||
53 | // The angle particles should fly |
||
54 | yAng# = 90+rnd(SPREAD2_MAX*2)-SPREAD2_MAX+yoff |
||
55 | |||
56 | // Calculate directions |
||
57 | xSpeed# = cos(yAng#) |
||
58 | zSpeed# = sin(yAng#) |
||
59 | // Calculate speeds |
||
60 | xSpeed# = xSpeed# * (rnd(20)/5.0) |
||
61 | ySpeed# = ((rnd(20)-rnd(20))/10.0) |
||
62 | zSpeed# = zSpeed# * (rnd(20)/5.0) |
||
63 | // Activate particle |
||
64 | |||
65 | x# = camera position x() |
||
66 | y# = camera position y() |
||
67 | z# = camera position z() |
||
68 | |||
69 | particleIndex = ActivateParticle(particleIndex, 30+rnd(30), x#, y#, z#, xSpeed#, ySpeed#, zSpeed#) |
||
70 | next i |
||
71 | return |
||
72 | |||
73 | // Activate the specified particle and give it an initial position/velocity |
||
74 | function ActivateParticle(i, life, x#, y#, z#, xSpeed#, ySpeed#, zSpeed#) |
||
75 | // Cycle through the particle objects to use |
||
76 | inc i |
||
77 | if i > PARTICLEMAX then dec i, PARTICLEMAX |
||
78 | |||
79 | // Set particle variables |
||
80 | particle(i).life = life |
||
81 | particle(i).x = x# |
||
82 | particle(i).y = y# |
||
83 | particle(i).z = z# |
||
84 | particle(i).xSpeed = xSpeed# |
||
85 | particle(i).ySpeed = ySpeed# |
||
86 | particle(i).zSpeed = zSpeed# |
||
87 | |||
88 | // Show particle object |
||
89 | exclude object off i |
||
90 | endfunction i |
||
91 | |||
92 | // Update positioning for specified particle |
||
93 | function UpdateParticle(i) |
||
94 | // Only update particles that are alive |
||
95 | if particle(i).life > 0 |
||
96 | |||
97 | // Apply gravity |
||
98 | dec particle(i).ySpeed, 0.098 |
||
99 | |||
100 | // Move the particle |
||
101 | inc particle(i).x, particle(i).xSpeed |
||
102 | inc particle(i).y, particle(i).ySpeed |
||
103 | inc particle(i).z, particle(i).zSpeed |
||
104 | |||
105 | // Position particle |
||
106 | position object i, particle(i).x, particle(i).y, particle(i).z |
||
107 | point object i, camera position x(), camera position y(), camera position z() |
||
108 | |||
109 | // Lower particle's life |
||
110 | dec particle(i).life |
||
111 | |||
112 | // Hide particles when they die |
||
113 | if particle(i).life <= 0 then exclude object on i |
||
114 | |||
115 | endif |
||
116 | endfunction |
||
117 | |||
118 | // Data type used by particles |
||
119 | type particleType |
||
120 | life as integer // Remaining life of particle (0=Dead) |
||
121 | x as float // Position |
||
122 | y as float // ... |
||
123 | z as float // ... |
||
124 | xSpeed as float // Velocity |
||
125 | ySpeed as float // ... |
||
126 | zSpeed as float // ... |
||
127 | endtype |