sevenx_engine 0.2.11

Engine de jogos 2D/3D completa com suporte Android, física, áudio, partículas, tilemap, UI, eventos e sistema 3D avançado com PBR.
Documentation
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use rand::Rng;

/// Representa uma partícula individual.
#[derive(Debug, Clone)]
pub struct Particle {
    pub x: f32,
    pub y: f32,
    pub vx: f32,
    pub vy: f32,
    pub life: f32,
    pub max_life: f32,
    pub color: [u8; 4],
    pub size: f32,
    pub rotation: f32,
    pub rotation_speed: f32,
    pub scale: f32,
    pub scale_speed: f32,
    pub fade_in: f32,
    pub fade_out: f32,
    pub acceleration_x: f32,
    pub acceleration_y: f32,
    pub color_shift: [f32; 4],
    pub turbulence: f32,
}

impl Particle {
    pub fn new(x: f32, y: f32, vx: f32, vy: f32, life: f32, color: [u8; 4], size: f32) -> Self {
        Self {
            x,
            y,
            vx,
            vy,
            life,
            max_life: life,
            color,
            size,
            rotation: 0.0,
            rotation_speed: 0.0,
            scale: 1.0,
            scale_speed: 0.0,
            fade_in: 0.0,
            fade_out: 0.2,
            acceleration_x: 0.0,
            acceleration_y: 0.0,
            color_shift: [0.0, 0.0, 0.0, 0.0],
            turbulence: 0.0,
        }
    }

    pub fn with_rotation(mut self, rotation: f32, rotation_speed: f32) -> Self {
        self.rotation = rotation;
        self.rotation_speed = rotation_speed;
        self
    }

    pub fn with_scale(mut self, scale: f32, scale_speed: f32) -> Self {
        self.scale = scale;
        self.scale_speed = scale_speed;
        self
    }

    pub fn with_fade(mut self, fade_in: f32, fade_out: f32) -> Self {
        self.fade_in = fade_in;
        self.fade_out = fade_out;
        self
    }

    pub fn with_acceleration(mut self, ax: f32, ay: f32) -> Self {
        self.acceleration_x = ax;
        self.acceleration_y = ay;
        self
    }

    pub fn with_color_shift(mut self, shift: [f32; 4]) -> Self {
        self.color_shift = shift;
        self
    }

    pub fn with_turbulence(mut self, turbulence: f32) -> Self {
        self.turbulence = turbulence;
        self
    }

    pub fn update(&mut self, dt: f32, gravity: f32, drag: f32) {
        // Aplica aceleração
        self.vx += self.acceleration_x * dt;
        self.vy += self.acceleration_y * dt;
        
        // Aplica turbulência
        if self.turbulence > 0.0 {
            use rand::Rng;
            let mut rng = rand::thread_rng();
            self.vx += rng.gen_range(-self.turbulence..self.turbulence);
            self.vy += rng.gen_range(-self.turbulence..self.turbulence);
        }
        
        // Atualiza posição
        self.x += self.vx * dt;
        self.y += self.vy * dt;
        self.vy += gravity * dt;
        
        // Aplica arrasto
        self.vx *= 1.0 - drag * dt;
        self.vy *= 1.0 - drag * dt;
        
        // Atualiza rotação
        self.rotation += self.rotation_speed * dt;
        
        // Atualiza escala
        self.scale += self.scale_speed * dt;
        self.scale = self.scale.max(0.0);
        
        // Atualiza cor
        let life_ratio = self.life / self.max_life;
        for i in 0..3 {
            let shift = self.color_shift[i] * (1.0 - life_ratio);
            self.color[i] = (self.color[i] as f32 + shift).clamp(0.0, 255.0) as u8;
        }
        
        self.life -= dt;
    }

    pub fn is_alive(&self) -> bool {
        self.life > 0.0
    }

    pub fn alpha(&self) -> u8 {
        let life_ratio = self.life / self.max_life;
        let age = 1.0 - life_ratio;
        
        let mut alpha = 1.0;
        
        // Fade in
        if age < self.fade_in {
            alpha = age / self.fade_in;
        }
        
        // Fade out
        if life_ratio < self.fade_out {
            alpha = alpha.min(life_ratio / self.fade_out);
        }
        
        (alpha * 255.0) as u8
    }

    pub fn get_size(&self) -> f32 {
        self.size * self.scale
    }
}

/// Sistema de partículas aprimorado.
pub struct ParticleSystem {
    pub particles: Vec<Particle>,
    pub max_particles: usize,
    pub drag: f32,
    pub blend_mode: BlendMode,
    pub global_gravity: f32,
    pub wind: (f32, f32),
}

/// Modos de blend para partículas.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BlendMode {
    Normal,
    Additive,
    Multiply,
}

impl ParticleSystem {
    pub fn new(max_particles: usize) -> Self {
        Self {
            particles: Vec::with_capacity(max_particles),
            max_particles,
            drag: 0.0,
            blend_mode: BlendMode::Normal,
            global_gravity: 0.0,
            wind: (0.0, 0.0),
        }
    }

    pub fn with_drag(mut self, drag: f32) -> Self {
        self.drag = drag.clamp(0.0, 1.0);
        self
    }

    pub fn with_blend_mode(mut self, blend_mode: BlendMode) -> Self {
        self.blend_mode = blend_mode;
        self
    }

    pub fn with_wind(mut self, wind_x: f32, wind_y: f32) -> Self {
        self.wind = (wind_x, wind_y);
        self
    }

    /// Emite uma única partícula.
    pub fn emit(&mut self, particle: Particle) {
        if self.particles.len() < self.max_particles {
            self.particles.push(particle);
        }
    }

    /// Emite uma explosão de partículas.
    pub fn emit_burst(&mut self, x: f32, y: f32, count: usize, config: ParticleConfig) {
        let mut rng = rand::thread_rng();

        for _ in 0..count.min(self.max_particles - self.particles.len()) {
            let angle = rng.gen_range(0.0..std::f32::consts::TAU);
            let speed = rng.gen_range(config.min_speed..config.max_speed);
            let vx = angle.cos() * speed;
            let vy = angle.sin() * speed;
            let life = rng.gen_range(config.min_life..config.max_life);
            let size = rng.gen_range(config.min_size..config.max_size);

            let particle = Particle::new(x, y, vx, vy, life, config.color, size);
            self.particles.push(particle);
        }
    }

    /// Emite partículas continuamente.
    pub fn emit_stream(&mut self, x: f32, y: f32, rate: f32, dt: f32, config: ParticleConfig) {
        let count = (rate * dt) as usize;
        self.emit_burst(x, y, count, config);
    }

    /// Atualiza todas as partículas.
    pub fn update(&mut self, dt: f32, gravity: f32) {
        let total_gravity = gravity + self.global_gravity;
        
        for particle in &mut self.particles {
            // Aplica vento
            particle.vx += self.wind.0 * dt;
            particle.vy += self.wind.1 * dt;
            
            particle.update(dt, total_gravity, self.drag);
        }

        // Remove partículas mortas
        self.particles.retain(|p| p.is_alive());
    }

    /// Renderiza as partículas com blend modes.
    pub fn render(&self, pixels: &mut [u8], camera_x: f32, camera_y: f32, viewport_width: u32, viewport_height: u32) {
        for particle in &self.particles {
            let screen_x = (particle.x - camera_x) as i32;
            let screen_y = (particle.y - camera_y) as i32;

            let size = particle.get_size();
            let half_size = (size / 2.0) as i32;

            for dy in -half_size..=half_size {
                for dx in -half_size..=half_size {
                    let px = screen_x + dx;
                    let py = screen_y + dy;

                    if px < 0 || py < 0 || px >= viewport_width as i32 || py >= viewport_height as i32 {
                        continue;
                    }

                    // Desenha círculo
                    let dist_sq = dx * dx + dy * dy;
                    if dist_sq <= half_size * half_size {
                        let index = ((py as u32 * viewport_width) + px as u32) as usize * 4;
                        if index + 3 < pixels.len() {
                            let mut color = particle.color;
                            color[3] = particle.alpha();
                            
                            // Soft edge
                            let edge_factor = 1.0 - (dist_sq as f32 / (half_size * half_size) as f32).sqrt();
                            color[3] = (color[3] as f32 * edge_factor) as u8;
                            
                            self.blend_pixel(&mut pixels[index..index + 4], &color);
                        }
                    }
                }
            }
        }
    }

    fn blend_pixel(&self, dest: &mut [u8], src: &[u8; 4]) {
        let alpha = src[3] as f32 / 255.0;
        
        match self.blend_mode {
            BlendMode::Normal => {
                for i in 0..3 {
                    dest[i] = ((src[i] as f32 * alpha) + (dest[i] as f32 * (1.0 - alpha))) as u8;
                }
            }
            BlendMode::Additive => {
                for i in 0..3 {
                    dest[i] = ((dest[i] as f32 + src[i] as f32 * alpha).min(255.0)) as u8;
                }
            }
            BlendMode::Multiply => {
                for i in 0..3 {
                    let blend = (dest[i] as f32 * src[i] as f32 / 255.0) as u8;
                    dest[i] = ((blend as f32 * alpha) + (dest[i] as f32 * (1.0 - alpha))) as u8;
                }
            }
        }
    }

    /// Limpa todas as partículas.
    pub fn clear(&mut self) {
        self.particles.clear();
    }

    /// Retorna o número de partículas ativas.
    pub fn count(&self) -> usize {
        self.particles.len()
    }
}

/// Configuração para emissão de partículas.
#[derive(Debug, Clone, Copy)]
pub struct ParticleConfig {
    pub color: [u8; 4],
    pub min_speed: f32,
    pub max_speed: f32,
    pub min_life: f32,
    pub max_life: f32,
    pub min_size: f32,
    pub max_size: f32,
}

impl ParticleConfig {
    pub fn new(color: [u8; 4]) -> Self {
        Self {
            color,
            min_speed: 50.0,
            max_speed: 150.0,
            min_life: 0.5,
            max_life: 2.0,
            min_size: 2.0,
            max_size: 4.0,
        }
    }

    pub fn explosion() -> Self {
        Self::new([255, 200, 0, 255])
            .with_speed(100.0, 300.0)
            .with_life(0.3, 1.0)
            .with_size(3.0, 6.0)
    }

    pub fn smoke() -> Self {
        Self::new([100, 100, 100, 200])
            .with_speed(10.0, 30.0)
            .with_life(1.0, 3.0)
            .with_size(4.0, 8.0)
    }

    pub fn sparkle() -> Self {
        Self::new([255, 255, 255, 255])
            .with_speed(20.0, 80.0)
            .with_life(0.2, 0.8)
            .with_size(1.0, 3.0)
    }

    pub fn fire() -> Self {
        Self::new([255, 100, 0, 255])
            .with_speed(5.0, 20.0)
            .with_life(0.5, 1.5)
            .with_size(3.0, 6.0)
    }

    pub fn magic() -> Self {
        Self::new([200, 100, 255, 255])
            .with_speed(30.0, 80.0)
            .with_life(0.5, 1.5)
            .with_size(2.0, 5.0)
    }

    pub fn blood() -> Self {
        Self::new([180, 0, 0, 255])
            .with_speed(50.0, 150.0)
            .with_life(0.3, 0.8)
            .with_size(2.0, 4.0)
    }

    pub fn snow() -> Self {
        Self::new([255, 255, 255, 200])
            .with_speed(5.0, 15.0)
            .with_life(2.0, 5.0)
            .with_size(2.0, 4.0)
    }

    pub fn rain() -> Self {
        Self::new([100, 150, 255, 180])
            .with_speed(200.0, 300.0)
            .with_life(0.5, 1.0)
            .with_size(1.0, 2.0)
    }

    pub fn with_speed(mut self, min: f32, max: f32) -> Self {
        self.min_speed = min;
        self.max_speed = max;
        self
    }

    pub fn with_life(mut self, min: f32, max: f32) -> Self {
        self.min_life = min;
        self.max_life = max;
        self
    }

    pub fn with_size(mut self, min: f32, max: f32) -> Self {
        self.min_size = min;
        self.max_size = max;
        self
    }
}