local textures_b = {
    "amogus_entity.png",
    "amogus_entity_b.png",
    "amogus_entity_g.png",
    "amogus_entity_br.png"
}

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)
        minetest.chat_send_player(clicker:get_player_name(), "WHY ARE YOU SUCH SUSSY BAKA?")
    end,


    -- 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

    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)
    --------------------

    is_going_forward = true,
    block_lastly_in_front = false,
    rotation_velocity = 0,
    rotation_direction = nil,
    last_y_velocity = 0,


    on_activate = function(self, staticdata)
		local texture = textures_b[math.random(#textures_b)]
		self.object:set_properties({textures = {texture}})
        
        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
	end,


    on_step = function(self, dtime)

        -- play amogus sound randomly
        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
            )
        end
        
        
        -- random changing between walking and standing still
        if math.random(200) == 1 then
            self.is_going_forward = not self.is_going_forward
        end

        -- perform calculations on direction
        local dir = self.object:get_yaw()
        local dir_cos = math.cos(dir)
        local dir_sin = math.sin(dir)

        -- Go forward if "is_going_forward" is true
        if self.is_going_forward then
            --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
            self.object:set_velocity(vel)
        end


        -- 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()

            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

        -- update rotation_velocity
        self.rotation_velocity = self.rotation_velocity + self.rotation_direction * self.rotation_acceleration_speed * dtime

        -- 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)

        
        -- Make it also jump when some block is in front of it
        local pos = self.object:get_pos()

        pos.x = pos.x + dir_cos
        pos.z = pos.z + dir_sin
        
        local bnode = minetest.get_node(pos)
        if bnode.name == "air" or bnode.name == "amogus_blocks:water" then
            self.block_lastly_in_front = false
        else
            if self.block_lastly_in_front == false then
                self.block_lastly_in_front = true
                local vel = self.object:get_velocity()
                vel.y = vel.y + (math.random() * self.min_max_jump_force_diff + self.min_jump_force)
                self.object:set_velocity(vel)
            end
        end


        
        local vel = self.object:get_velocity()

        -- 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

        -- 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)

        self.object:set_velocity(vel)

        self.last_y_velocity = vel.y
    end
}

minetest.register_entity("amogus_entities:amogus", entity)