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    /// İki dövüşçüyü aynı anda takip eden fighting camera
96    SetFightCamera {
97        p1_id: u32,
98        p2_id: u32,
99        height: f32,     // Kamera yüksekliği (Y offset)
100        distance: f32,   // Minimum uzaklık (Z offset)
101    },
102
103// Component
104    SetEntityName(u32, String),
105PlayAnimation {
106        id: u32,
107        name: String,
108        blend: f32,
109        loop_anim: bool,
110    },
111    SetAnimationSpeed(u32, f32),
112
113
114    // AI
115    AddNavAgent(u32),
116    SetAiTarget(u32, Vec3),
117    ClearAiTarget(u32),
118
119    // Fighter
120    SetFighterMove {
121        id: u32,
122        name: String,
123        startup: u32,
124        active: u32,
125        recovery: u32,
126        damage: f32,
127    },
128    ApplyHitstop(u32, u32),
129    ApplyHitstun(u32, u32),
130}
131
132
133/// Thread-local komut kuyruğu (Lua callback'leri içinden erişilebilir)
134pub struct CommandQueue {
135    pub commands: Mutex<Vec<ScriptCommand>>,
136}
137
138impl CommandQueue {
139    pub fn new() -> Self {
140        Self {
141            commands: Mutex::new(Vec::new()),
142        }
143    }
144
145    pub fn push(&self, cmd: ScriptCommand) {
146        self.commands.lock().unwrap().push(cmd);
147    }
148
149    pub fn drain(&self) -> Vec<ScriptCommand> {
150        self.commands.lock().unwrap().drain(..).collect()
151    }
152
153    pub fn is_empty(&self) -> bool {
154        self.commands.lock().unwrap().is_empty()
155    }
156
157    pub fn len(&self) -> usize {
158        self.commands.lock().unwrap().len()
159    }
160}
161
162impl Default for CommandQueue {
163    fn default() -> Self {
164        Self::new()
165    }
166}