dcso3/
spot.rs

1/*
2Copyright 2024 Eric Stokes.
3
4This file is part of dcso3.
5
6dcso3 is free software: you can redistribute it and/or modify it under
7the terms of the MIT License.
8
9dcso3 is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE.
12*/
13
14use super::{as_tbl, object::Object};
15use crate::{
16    object::{DcsObject, DcsOid},
17    wrapped_table, LuaEnv, LuaVec3, MizLua,
18};
19use anyhow::Result;
20use mlua::{prelude::*, Value};
21use serde_derive::Serialize;
22use std::{marker::PhantomData, ops::Deref};
23
24wrapped_table!(Spot, Some("Spot"));
25
26impl<'lua> Spot<'lua> {
27    pub fn create_laser(
28        lua: MizLua<'lua>,
29        source: Object<'lua>,
30        local_ref: Option<LuaVec3>,
31        target: LuaVec3,
32        code: u16,
33    ) -> Result<Self> {
34        let globals = lua.inner().globals();
35        let spot: LuaTable = globals.raw_get("Spot")?;
36        Ok(spot.call_function("createLaser", (source, local_ref, target, code))?)
37    }
38
39    pub fn create_infra_red(
40        lua: MizLua<'lua>,
41        source: Object<'lua>,
42        local_ref: Option<LuaVec3>,
43        target: LuaVec3,
44    ) -> Result<Self> {
45        let globals = lua.inner().globals();
46        let spot: LuaTable = globals.raw_get("Spot")?;
47        Ok(spot.call_function("createInfraRed", (source, local_ref, target))?)
48    }
49
50    pub fn destroy(self) -> Result<()> {
51        Ok(self.t.call_method("destroy", ())?)
52    }
53
54    pub fn get_point(&self) -> Result<LuaVec3> {
55        Ok(self.t.call_method("getPoint", ())?)
56    }
57
58    pub fn set_point(&self, target: LuaVec3) -> Result<()> {
59        Ok(self.t.call_method("setPoint", target)?)
60    }
61
62    pub fn get_code(&self) -> Result<u16> {
63        Ok(self.t.call_method("getCode", ())?)
64    }
65
66    pub fn set_code(&self, code: u16) -> Result<()> {
67        Ok(self.t.call_method("setCode", code)?)
68    }
69}
70
71#[derive(Debug, Clone)]
72pub struct ClassSpot;
73
74impl<'lua> DcsObject<'lua> for Spot<'lua> {
75    type Class = ClassSpot;
76
77    fn get_instance(lua: MizLua<'lua>, id: &DcsOid<Self::Class>) -> Result<Self> {
78        let t = lua.inner().create_table()?;
79        t.set_metatable(Some(lua.inner().globals().raw_get(&**id.class)?));
80        t.raw_set("id_", id.id)?;
81        let t = Self {
82            t,
83            lua: lua.inner(),
84        };
85        Ok(t)
86    }
87
88    fn get_instance_dyn<T>(lua: MizLua<'lua>, id: &DcsOid<T>) -> Result<Self> {
89        id.check_implements(lua, "Spot")?;
90        let id = DcsOid {
91            id: id.id,
92            class: id.class.clone(),
93            t: PhantomData,
94        };
95        Self::get_instance(lua, &id)
96    }
97
98    fn change_instance(self, id: &DcsOid<Self::Class>) -> Result<Self> {
99        self.raw_set("id_", id.id)?;
100        Ok(self)
101    }
102
103    fn change_instance_dyn<T>(self, id: &DcsOid<T>) -> Result<Self> {
104        id.check_implements(MizLua(self.lua), "Spot")?;
105        self.t.raw_set("id_", id.id)?;
106        Ok(self)
107    }
108}