Compare commits

...

2 Commits

View File

@ -50,6 +50,18 @@ local entity = {
sound_pitch = nil,
sound_propability = nil,
-- player detection variables
player = nil,
player_pos = nil,
mini_crewmate_pos = nil,
crewmate_player_vector = nil,
distance = nil,
current_distance = nil,
current_crewmate_player_vector = nil,
mode = 0, -- | 0 - stand still | 1 - walk | 2 - chase | 3 - run away | 4 - rest mode |
@ -93,46 +105,65 @@ local entity = {
})
end
--local player_count = #minetest.get_connected_players()
--if player_count == 1 then
-- game is in singleplayer mode or there is only one player
-- if on singpleplayer mode or only one player is connected, calculate distance between player and mini crewmate
if minetest.is_singleplayer() or #minetest.get_connected_players() == 1 then
minetest.chat_send_all("singleplayer or multiplayer with only one player")
-- calculate distance between "singleplayer" player and mini crewmate
local player = minetest.get_player_by_name("singleplayer")
local player_pos = player:get_pos()
local mini_crewmate_pos = self.object:get_pos()
local crewmate_player_vector = vector.subtract(player_pos, mini_crewmate_pos)
--local distance = vector.distance(player_pos, mini_crewmate_pos)
local distance = vector.length(crewmate_player_vector)
-- get first player
self.player = minetest.get_connected_players()[1]
self.player_pos = self.player:get_pos()
self.mini_crewmate_pos = self.object:get_pos()
self.crewmate_player_vector = vector.subtract(self.player_pos, self.mini_crewmate_pos)
self.distance = vector.length(self.crewmate_player_vector)
-- if on multiplayer mode, calculate distance between nearest player and mini crewmate
else
minetest.chat_send_all("multiplayer")
self.player = minetest.get_connected_players()
--minetest.chat_send_player("singleplayer", "HSUSS")
--else
-- game is in multiplayer mode with more than one player
self.distance = nil
self.crewmate_player_vector = nil
-- I will write it later.
--end
self.mini_crewmate_pos = self.object:get_pos()
-- iterate over all players
for i = 1, #self.player do
minetest.chat_send_all("player " .. i)
self.player_pos = self.player[i]:get_pos()
--self.crewmate_player_vector = vector.subtract(self.player_pos, self.mini_crewmate_pos)
self.current_crewmate_player_vector = vector.subtract(self.player_pos, self.mini_crewmate_pos)
self.current_distance = vector.length(self.current_crewmate_player_vector)
-- if distance is nil or current_distance is smaller than distance, set distance to current_distance
if self.distance == nil or self.current_distance < self.distance then
self.distance = self.current_distance
self.crewmate_player_vector = self.current_crewmate_player_vector
end
end
end
-- if mode is 0 or 1 (stand still or walk) and distance is smaller or equal to largest_distance_to_find_player, set mode to 2 (chase)
if (self.mode == 0 or self.mode == 1) and distance <= self.largest_distance_to_find_player then
if (self.mode == 0 or self.mode == 1) and self.distance <= self.largest_distance_to_find_player then
self.mode = 2
-- elseif mode is 2 (chase)
elseif self.mode == 2 then
-- if distance is greater or equal to smallest_distance_to_lost_player, set mode to 0 (stand still)
if distance >= self.smallest_distance_to_lost_player then
if self.distance >= self.smallest_distance_to_lost_player then
self.mode = 0
-- elif distance is smaller or equal to largest_distance_to_stand_still, set mode to 4 (rest mode)
elseif distance <= self.largest_distance_to_stand_still then
elseif self.distance <= self.largest_distance_to_stand_still then
self.mode = 4
end
-- if mode is 4 (rest mode) and distance is greater than largest_distance_to_stand_still, set mode to 2 (chase)
elseif self.mode == 4 and distance > self.largest_distance_to_stand_still then
elseif self.mode == 4 and self.distance > self.largest_distance_to_stand_still then
self.mode = 2
-- running away mode
elseif self.mode == 3 then
-- if distance is greater than smallest_distance_to_lost_player, set mode to 0 (stand still)
if distance > self.smallest_distance_to_stop_run_away then
if self.distance > self.smallest_distance_to_stop_run_away then
self.mode = 1
else
-- jump if on ground
@ -189,10 +220,10 @@ local entity = {
-- if mode is 3 (run away) set rotation so mini crewmate is facing either at the player or away from the player depending on situation
if self.mode == 3 then
-- look away from the player
self.object:set_yaw(math.atan2(-crewmate_player_vector.z, -crewmate_player_vector.x))
self.object:set_yaw(math.atan2(-self.crewmate_player_vector.z, -self.crewmate_player_vector.x))
else
-- look at the player
self.object:set_yaw(math.atan2(crewmate_player_vector.z, crewmate_player_vector.x))
self.object:set_yaw(math.atan2(self.crewmate_player_vector.z, self.crewmate_player_vector.x))
end
end