mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
Implement Terrain.GetMaterialColor and Terrain.SetMaterialColor
This commit is contained in:
parent
5aa9e53c64
commit
b8de95eb1e
2 changed files with 129 additions and 0 deletions
|
@ -20,6 +20,7 @@ use crate::{
|
||||||
|
|
||||||
pub(crate) mod base;
|
pub(crate) mod base;
|
||||||
pub(crate) mod data_model;
|
pub(crate) mod data_model;
|
||||||
|
pub(crate) mod terrain;
|
||||||
pub(crate) mod workspace;
|
pub(crate) mod workspace;
|
||||||
|
|
||||||
const PROPERTY_NAME_ATTRIBUTES: &str = "Attributes";
|
const PROPERTY_NAME_ATTRIBUTES: &str = "Attributes";
|
||||||
|
@ -729,6 +730,7 @@ impl LuaUserData for Instance {
|
||||||
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
base::add_methods(methods);
|
base::add_methods(methods);
|
||||||
data_model::add_methods(methods);
|
data_model::add_methods(methods);
|
||||||
|
terrain::add_methods(methods);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
127
src/roblox/instance/terrain.rs
Normal file
127
src/roblox/instance/terrain.rs
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
use mlua::prelude::*;
|
||||||
|
use rbx_dom_weak::types::{MaterialColors, TerrainMaterials, Variant};
|
||||||
|
|
||||||
|
use crate::roblox::{
|
||||||
|
datatypes::types::{Color3, EnumItem},
|
||||||
|
shared::classes::{add_class_restricted_method, add_class_restricted_method_mut},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::Instance;
|
||||||
|
|
||||||
|
pub const CLASS_NAME: &str = "Terrain";
|
||||||
|
|
||||||
|
fn material_from_name(material_name: &str) -> Option<TerrainMaterials> {
|
||||||
|
match material_name {
|
||||||
|
"Grass" => Some(TerrainMaterials::Grass),
|
||||||
|
"Slate" => Some(TerrainMaterials::Slate),
|
||||||
|
"Concrete" => Some(TerrainMaterials::Concrete),
|
||||||
|
"Brick" => Some(TerrainMaterials::Brick),
|
||||||
|
"Sand" => Some(TerrainMaterials::Sand),
|
||||||
|
"WoodPlanks" => Some(TerrainMaterials::WoodPlanks),
|
||||||
|
"Rock" => Some(TerrainMaterials::Rock),
|
||||||
|
"Glacier" => Some(TerrainMaterials::Glacier),
|
||||||
|
"Snow" => Some(TerrainMaterials::Snow),
|
||||||
|
"Sandstone" => Some(TerrainMaterials::Sandstone),
|
||||||
|
"Mud" => Some(TerrainMaterials::Mud),
|
||||||
|
"Basalt" => Some(TerrainMaterials::Basalt),
|
||||||
|
"Ground" => Some(TerrainMaterials::Ground),
|
||||||
|
"CrackedLava" => Some(TerrainMaterials::CrackedLava),
|
||||||
|
"Asphalt" => Some(TerrainMaterials::Asphalt),
|
||||||
|
"Cobblestone" => Some(TerrainMaterials::Cobblestone),
|
||||||
|
"Ice" => Some(TerrainMaterials::Ice),
|
||||||
|
"LeafyGrass" => Some(TerrainMaterials::LeafyGrass),
|
||||||
|
"Salt" => Some(TerrainMaterials::Salt),
|
||||||
|
"Limestone" => Some(TerrainMaterials::Limestone),
|
||||||
|
"Pavement" => Some(TerrainMaterials::Pavement),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_methods<'lua, M: LuaUserDataMethods<'lua, Instance>>(methods: &mut M) {
|
||||||
|
add_class_restricted_method(
|
||||||
|
methods,
|
||||||
|
CLASS_NAME,
|
||||||
|
"GetMaterialColor",
|
||||||
|
terrain_get_material_color,
|
||||||
|
);
|
||||||
|
|
||||||
|
add_class_restricted_method_mut(
|
||||||
|
methods,
|
||||||
|
CLASS_NAME,
|
||||||
|
"SetMaterialColor",
|
||||||
|
terrain_set_material_color,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_or_create_material_colors(instance: &Instance) -> MaterialColors {
|
||||||
|
if let Some(Variant::MaterialColors(material_colors)) = instance.get_property("MaterialColors")
|
||||||
|
{
|
||||||
|
material_colors
|
||||||
|
} else {
|
||||||
|
MaterialColors::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns the color of the given terrain material.
|
||||||
|
|
||||||
|
### See Also
|
||||||
|
* [`GetMaterialColor`](https://create.roblox.com/docs/reference/engine/classes/Terrain#GetMaterialColor)
|
||||||
|
on the Roblox Developer Hub
|
||||||
|
*/
|
||||||
|
fn terrain_get_material_color(_: &Lua, this: &Instance, material: EnumItem) -> LuaResult<Color3> {
|
||||||
|
let material_colors = get_or_create_material_colors(this);
|
||||||
|
|
||||||
|
if &material.parent.desc.name != "Material" {
|
||||||
|
return Err(LuaError::RuntimeError(format!(
|
||||||
|
"Expected Material, got {}",
|
||||||
|
&material.parent.desc.name
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(terrain_material) = material_from_name(&material.name) {
|
||||||
|
Ok(material_colors.get_color(terrain_material).into())
|
||||||
|
} else {
|
||||||
|
Err(LuaError::RuntimeError(format!(
|
||||||
|
"{} is not a valid Terrain material",
|
||||||
|
&material.name
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Sets the color of the given terrain material.
|
||||||
|
|
||||||
|
### See Also
|
||||||
|
* [`SetMaterialColor`](https://create.roblox.com/docs/reference/engine/classes/Terrain#SetMaterialColor)
|
||||||
|
on the Roblox Developer Hub
|
||||||
|
*/
|
||||||
|
fn terrain_set_material_color(
|
||||||
|
_: &Lua,
|
||||||
|
this: &mut Instance,
|
||||||
|
args: (EnumItem, Color3),
|
||||||
|
) -> LuaResult<()> {
|
||||||
|
let mut material_colors = get_or_create_material_colors(this);
|
||||||
|
let material = args.0;
|
||||||
|
let color = args.1;
|
||||||
|
|
||||||
|
if &material.parent.desc.name != "Material" {
|
||||||
|
return Err(LuaError::RuntimeError(format!(
|
||||||
|
"Expected Material, got {}",
|
||||||
|
&material.parent.desc.name
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let terrain_material = if let Some(terrain_material) = material_from_name(&material.name) {
|
||||||
|
terrain_material
|
||||||
|
} else {
|
||||||
|
return Err(LuaError::RuntimeError(format!(
|
||||||
|
"{} is not a valid Terrain material",
|
||||||
|
&material.name
|
||||||
|
)));
|
||||||
|
};
|
||||||
|
|
||||||
|
material_colors.set_color(terrain_material, color.into());
|
||||||
|
this.set_property("MaterialColors", Variant::MaterialColors(material_colors));
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue