mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Add Lua methods 'set_rotation()' and 'get_rotation()' (#7395)
* Adds Lua methods 'set_rotation()' and 'get_rotation'. Also changed some method names to be more clear. Instead of an f32 being sent over network for yaw, now a v3f is sent for rotation on xyz axes. Perserved Lua method set_yaw/setyaw so that old mods still work, other wise to set yaw they would need to switch to set_rotation(0, yaw, 0).
This commit is contained in:
parent
9519d57017
commit
faa358e797
18 changed files with 211 additions and 99 deletions
|
@ -889,19 +889,51 @@ int ObjectRef::l_get_acceleration(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
// set_rotation(self, {x=num, y=num, z=num})
|
||||
// Each 'num' is in radians
|
||||
int ObjectRef::l_set_rotation(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
ObjectRef *ref = checkobject(L, 1);
|
||||
LuaEntitySAO *co = getluaobject(ref);
|
||||
if (!co)
|
||||
return 0;
|
||||
|
||||
v3f rotation = check_v3f(L, 2) * core::RADTODEG;
|
||||
co->setRotation(rotation);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// get_rotation(self)
|
||||
// returns: {x=num, y=num, z=num}
|
||||
// Each 'num' is in radians
|
||||
int ObjectRef::l_get_rotation(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
ObjectRef *ref = checkobject(L, 1);
|
||||
LuaEntitySAO *co = getluaobject(ref);
|
||||
if (!co)
|
||||
return 0;
|
||||
|
||||
lua_newtable(L);
|
||||
v3f rotation = co->getRotation() * core::DEGTORAD;
|
||||
push_v3f(L, rotation);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// set_yaw(self, radians)
|
||||
int ObjectRef::l_set_yaw(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
ObjectRef *ref = checkobject(L, 1);
|
||||
LuaEntitySAO *co = getluaobject(ref);
|
||||
|
||||
if (co == NULL) return 0;
|
||||
if (isNaN(L, 2))
|
||||
throw LuaError("ObjectRef::set_yaw: NaN value is not allowed.");
|
||||
|
||||
float yaw = readParam<float>(L, 2) * core::RADTODEG;
|
||||
// Do it
|
||||
co->setYaw(yaw);
|
||||
co->setRotation(v3f(0, yaw, 0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -911,9 +943,10 @@ int ObjectRef::l_get_yaw(lua_State *L)
|
|||
NO_MAP_LOCK_REQUIRED;
|
||||
ObjectRef *ref = checkobject(L, 1);
|
||||
LuaEntitySAO *co = getluaobject(ref);
|
||||
if (co == NULL) return 0;
|
||||
// Do it
|
||||
float yaw = co->getYaw() * core::DEGTORAD;
|
||||
if (!co)
|
||||
return 0;
|
||||
|
||||
float yaw = co->getRotation().Y * core::DEGTORAD;
|
||||
lua_pushnumber(L, yaw);
|
||||
return 1;
|
||||
}
|
||||
|
@ -1046,7 +1079,7 @@ int ObjectRef::l_get_look_dir(lua_State *L)
|
|||
PlayerSAO* co = getplayersao(ref);
|
||||
if (co == NULL) return 0;
|
||||
// Do it
|
||||
float pitch = co->getRadPitchDep();
|
||||
float pitch = co->getRadLookPitchDep();
|
||||
float yaw = co->getRadYawDep();
|
||||
v3f v(std::cos(pitch) * std::cos(yaw), std::sin(pitch), std::cos(pitch) *
|
||||
std::sin(yaw));
|
||||
|
@ -1067,7 +1100,7 @@ int ObjectRef::l_get_look_pitch(lua_State *L)
|
|||
PlayerSAO* co = getplayersao(ref);
|
||||
if (co == NULL) return 0;
|
||||
// Do it
|
||||
lua_pushnumber(L, co->getRadPitchDep());
|
||||
lua_pushnumber(L, co->getRadLookPitchDep());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -1096,7 +1129,7 @@ int ObjectRef::l_get_look_vertical(lua_State *L)
|
|||
PlayerSAO* co = getplayersao(ref);
|
||||
if (co == NULL) return 0;
|
||||
// Do it
|
||||
lua_pushnumber(L, co->getRadPitch());
|
||||
lua_pushnumber(L, co->getRadLookPitch());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -1108,7 +1141,7 @@ int ObjectRef::l_get_look_horizontal(lua_State *L)
|
|||
PlayerSAO* co = getplayersao(ref);
|
||||
if (co == NULL) return 0;
|
||||
// Do it
|
||||
lua_pushnumber(L, co->getRadYaw());
|
||||
lua_pushnumber(L, co->getRadRotation().Y);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -1121,7 +1154,7 @@ int ObjectRef::l_set_look_vertical(lua_State *L)
|
|||
if (co == NULL) return 0;
|
||||
float pitch = readParam<float>(L, 2) * core::RADTODEG;
|
||||
// Do it
|
||||
co->setPitchAndSend(pitch);
|
||||
co->setLookPitchAndSend(pitch);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -1134,7 +1167,7 @@ int ObjectRef::l_set_look_horizontal(lua_State *L)
|
|||
if (co == NULL) return 0;
|
||||
float yaw = readParam<float>(L, 2) * core::RADTODEG;
|
||||
// Do it
|
||||
co->setYawAndSend(yaw);
|
||||
co->setPlayerYawAndSend(yaw);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -1152,7 +1185,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L)
|
|||
if (co == NULL) return 0;
|
||||
float pitch = readParam<float>(L, 2) * core::RADTODEG;
|
||||
// Do it
|
||||
co->setPitchAndSend(pitch);
|
||||
co->setLookPitchAndSend(pitch);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -1170,7 +1203,7 @@ int ObjectRef::l_set_look_yaw(lua_State *L)
|
|||
if (co == NULL) return 0;
|
||||
float yaw = readParam<float>(L, 2) * core::RADTODEG;
|
||||
// Do it
|
||||
co->setYawAndSend(yaw);
|
||||
co->setPlayerYawAndSend(yaw);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -1861,6 +1894,8 @@ luaL_Reg ObjectRef::methods[] = {
|
|||
luamethod_aliased(ObjectRef, get_acceleration, getacceleration),
|
||||
luamethod_aliased(ObjectRef, set_yaw, setyaw),
|
||||
luamethod_aliased(ObjectRef, get_yaw, getyaw),
|
||||
luamethod(ObjectRef, set_rotation),
|
||||
luamethod(ObjectRef, get_rotation),
|
||||
luamethod_aliased(ObjectRef, set_texture_mod, settexturemod),
|
||||
luamethod_aliased(ObjectRef, set_sprite, setsprite),
|
||||
luamethod(ObjectRef, get_entity_name),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue