SussyCraft/mods/amogus_entities/entities/amogus_entity.lua

162 lines
5.4 KiB
Lua
Raw Normal View History

2023-01-29 08:25:10 +01:00
local textures_b = {
"amogus_entity.png",
"amogus_entity_b.png",
"amogus_entity_g.png",
"amogus_entity_br.png"
}
2023-01-08 00:11:47 +01:00
local entity = {
physical = true,
collisionbox = {-0.5, 0, -0.5, 0.5, 1, 0.5},
visual = "mesh",
visual_size = {x=10, y=10, z=10},
mesh = "amogus_entity.obj",
on_rightclick = function(self, clicker)
2023-01-08 02:32:49 +01:00
minetest.chat_send_player(clicker:get_player_name(), "WHY ARE YOU SUCH SUSSY BAKA?")
2023-01-08 00:11:47 +01:00
end,
2023-01-08 02:32:49 +01:00
-- physics config -- <--- HERE IS PART FOR YOU "USER" (NERD)
gravity = 9.81, -- m/s^2
walk_acceleration_speed = 0.2, -- self explainatory
rotation_acceleration_speed = 3, -- self explainatory
2023-01-08 02:32:49 +01:00
2023-01-08 15:25:36 +01:00
min_jump_force = 15, -- self explainatory
max_jump_force = 25, -- self explainatory
friction = 0.6, -- friction (0.0 - no friction like perfectly smooth ice | 1.0 - full friction and can't even move)
rotation_friction = 0.75, -- the same but for rotation
bounciness = 0.8, -- bounciness (0.0 - no bounce at all | 1.0 - full bounce and bouces infinitely back to the same height)
2023-01-08 02:32:49 +01:00
--------------------
2023-01-08 00:11:47 +01:00
is_going_forward = true,
2023-01-08 02:32:49 +01:00
block_lastly_in_front = false,
2023-01-08 15:25:36 +01:00
rotation_velocity = 0,
rotation_direction = nil,
last_y_velocity = 0,
2023-01-08 02:32:49 +01:00
2023-02-03 07:03:12 +01:00
2023-01-08 02:32:49 +01:00
on_activate = function(self, staticdata)
2023-01-29 08:25:10 +01:00
local texture = textures_b[math.random(#textures_b)]
self.object:set_properties({textures = {texture}})
2023-01-31 22:31:00 +01:00
2023-01-08 15:25:36 +01:00
self.object:set_yaw(math.random() * 2 * math.pi)
self.rotation_direction = math.random(-1, 1)
self.min_max_jump_force_diff = self.max_jump_force - self.min_jump_force
2023-01-08 02:32:49 +01:00
end,
2023-01-08 00:11:47 +01:00
on_step = function(self, dtime)
2023-01-08 02:32:49 +01:00
-- play amogus sound randomly
2023-01-29 00:09:39 +01:00
if math.random(400) == 1 then
--minetest.sound_play("amogus_sound", {
-- pos = self.object:get_pos(),
-- gain = 1.0,
-- max_hear_distance = 50
--})
amogus_general.play_random_sound(
"amogus_sound",
self.object:get_pos(),
1.0, -- gain
40, -- max_hear_distance
1.0 -- pitch
)
2023-01-08 02:32:49 +01:00
end
-- random changing between walking and standing still
2023-01-08 00:11:47 +01:00
if math.random(200) == 1 then
self.is_going_forward = not self.is_going_forward
end
2023-01-08 02:32:49 +01:00
-- perform calculations on direction
local dir = self.object:get_yaw()
local dir_cos = math.cos(dir)
local dir_sin = math.sin(dir)
2023-01-08 00:11:47 +01:00
-- Go forward if "is_going_forward" is true
if self.is_going_forward then
2023-01-08 02:32:49 +01:00
--local pos = self.object:get_pos()
local vel = self.object:get_velocity()
vel.x = vel.x + dir_cos * self.walk_acceleration_speed
vel.z = vel.z + dir_sin * self.walk_acceleration_speed
2023-01-08 02:32:49 +01:00
self.object:set_velocity(vel)
2023-01-08 00:11:47 +01:00
end
2023-01-08 15:25:36 +01:00
-- some chance of chaning rotation_direction to random value
if math.random(10) == 1 then
--self.rotation_direction = math.random(-1, 1)
local rand = math.random()
2023-01-08 02:32:49 +01:00
2023-01-08 15:25:36 +01:00
if rand < 0.2 then
self.rotation_direction = 1
elseif rand < 0.4 then
self.rotation_direction = -1
else
self.rotation_direction = 0
end
end
2023-01-08 02:32:49 +01:00
2023-01-08 15:25:36 +01:00
-- update rotation_velocity
self.rotation_velocity = self.rotation_velocity + self.rotation_direction * self.rotation_acceleration_speed * dtime
2023-01-08 15:25:36 +01:00
-- update rotation
self.object:set_yaw(self.object:get_yaw() + self.rotation_velocity * dtime)
-- apply rotation_friction
self.rotation_velocity = self.rotation_velocity * (1 - self.rotation_friction * dtime)
2023-01-08 02:32:49 +01:00
-- Make it also jump when some block is in front of it
2023-01-08 00:11:47 +01:00
local pos = self.object:get_pos()
2023-01-08 02:32:49 +01:00
pos.x = pos.x + dir_cos
pos.z = pos.z + dir_sin
2023-01-08 00:11:47 +01:00
local bnode = minetest.get_node(pos)
--if bnode.name == "air" or bnode.name == "amogus_blocks:water_source" or bnode.name == "amogus_blocks:water_flowing" then
-- if node is not solid OR is a liquid
if minetest.registered_nodes[bnode.name].walkable == false or minetest.registered_nodes[bnode.name].liquidtype ~= "none" then
self.block_lastly_in_front = false
-- if node next to the entity is a solid block
else
2023-01-08 02:32:49 +01:00
if self.block_lastly_in_front == false then
self.block_lastly_in_front = true
local vel = self.object:get_velocity()
2023-01-08 15:25:36 +01:00
vel.y = vel.y + (math.random() * self.min_max_jump_force_diff + self.min_jump_force)
2023-01-08 02:32:49 +01:00
self.object:set_velocity(vel)
-- randomly jump in the next iteration even if theres still a block in front
elseif math.random(10) == 1 then
self.block_lastly_in_front = false
2023-01-08 02:32:49 +01:00
end
2023-01-08 00:11:47 +01:00
end
2023-01-08 02:32:49 +01:00
2023-01-08 00:11:47 +01:00
local vel = self.object:get_velocity()
2023-01-08 02:32:49 +01:00
2023-01-08 15:25:36 +01:00
-- apply bounciness if last_y_velocity was negative and now is 0
if self.last_y_velocity < 0 and vel.y == 0 then
vel.y = vel.y - self.last_y_velocity * self.bounciness
end
2023-01-08 02:32:49 +01:00
-- change velocity by gravity
vel.y = vel.y - self.gravity * dtime * 2
-- change velocity by friction
vel.x = vel.x * (1 - self.friction * dtime)
vel.z = vel.z * (1 - self.friction * dtime)
2023-01-08 00:11:47 +01:00
self.object:set_velocity(vel)
2023-01-08 15:25:36 +01:00
self.last_y_velocity = vel.y
2023-01-08 00:11:47 +01:00
end
}
2023-01-08 00:11:47 +01:00
minetest.register_entity("amogus_entities:amogus", entity)