Skip to main content

gizmo_scripting/
commands.rs

1//! Script Command Queue — Lua scriptlerden gelen değişiklik isteklerinin biriktirildiği kuyruk
2//!
3//! Lua scriptleri doğrudan World'ü mutate edemez (Rust borrow kuralları).
4//! Bunun yerine komutlar bu kuyrukta birikir ve frame sonunda `flush()` ile uygulanır.
5
6use gizmo_math::{Quat, Vec3};
7use std::sync::Mutex;
8/// Lua'dan gelen tüm değişiklik istekleri
9#[derive(Debug, Clone)]
10pub enum ScriptCommand {
11    // Transform
12    SetPosition(u32, Vec3),
13    SetRotation(u32, Quat),
14    SetScale(u32, Vec3),
15
16    // Velocity
17    SetVelocity(u32, Vec3),
18    SetAngularVelocity(u32, Vec3),
19
20    // Physics
21    ApplyForce(u32, Vec3),
22    ApplyImpulse(u32, Vec3),
23    AddRigidBody {
24        id: u32,
25        mass: f32,
26        restitution: f32,
27        friction: f32,
28        use_gravity: bool,
29    },
30    AddBoxCollider {
31        id: u32,
32        hx: f32,
33        hy: f32,
34        hz: f32,
35    },
36    AddSphereCollider {
37        id: u32,
38        radius: f32,
39    },
40
41    // Vehicle
42    SetVehicleEngineForce(u32, f32),
43    SetVehicleSteering(u32, f32),
44    SetVehicleBrake(u32, f32),
45
46    // Entity Lifecycle
47    SpawnEntity {
48        name: String,
49        position: Vec3,
50    },
51    SpawnPrefab {
52        name: String,
53        prefab_type: String,
54        position: Vec3,
55    },
56    DestroyEntity(u32),
57
58    // Audio
59    PlaySound(String),
60    PlaySound3D(String, Vec3),
61    StopSound(String),
62
63    // Scene
64    LoadScene(String),
65    SaveScene(String),
66
67    // Diyalog Sistemi
68    ShowDialogue {
69        speaker: String,
70        text: String,
71        duration: f32,
72    },
73    HideDialogue,
74
75    // Ara Sahne (Cutscene)
76    TriggerCutscene(String), // cutscene adı/id
77    EndCutscene,
78
79    // Yarış Sistemi
80    StartRace,
81    AddCheckpoint {
82        id: u32,
83        position: Vec3,
84        radius: f32,
85    },
86    ActivateCheckpoint(u32),
87    FinishRace {
88        winner_name: String,
89    },
90    ResetRace,
91
92    // Kamera
93    SetCameraTarget(u32), // hangi entity'yi takip etsin
94    SetCameraFov(f32),
95
96    // Component
97    SetEntityName(u32, String),
98
99    // AI
100    AddNavAgent(u32),
101    SetAiTarget(u32, Vec3),
102    ClearAiTarget(u32),
103}
104
105/// Thread-local komut kuyruğu (Lua callback'leri içinden erişilebilir)
106pub struct CommandQueue {
107    pub commands: Mutex<Vec<ScriptCommand>>,
108}
109
110impl CommandQueue {
111    pub fn new() -> Self {
112        Self {
113            commands: Mutex::new(Vec::new()),
114        }
115    }
116
117    pub fn push(&self, cmd: ScriptCommand) {
118        self.commands.lock().unwrap().push(cmd);
119    }
120
121    pub fn drain(&self) -> Vec<ScriptCommand> {
122        self.commands.lock().unwrap().drain(..).collect()
123    }
124
125    pub fn is_empty(&self) -> bool {
126        self.commands.lock().unwrap().is_empty()
127    }
128
129    pub fn len(&self) -> usize {
130        self.commands.lock().unwrap().len()
131    }
132}
133
134impl Default for CommandQueue {
135    fn default() -> Self {
136        Self::new()
137    }
138}