Page 1 of 1

A simple mob

PostPosted: Mon Oct 19, 2015 01:14
by Don
I need a simple mob for a mini game I am making. I am wondering if it is better to use an existing mob api for this or write a new one.

The mob only needs to chase a player. There is no animation, punch or attack. I don't even want the player to be able to hit the mob.

I know very little about mobs so any advice would be appreciated.

Re: A simple mob

PostPosted: Mon Oct 19, 2015 19:57
by TenPlus1
For simplicity you could use the mods redo api but if the mob in question does nothing but follow the player then this code may be simple to implement, it spawns a cube that follows player.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local gravity = -10

minetest.register_entity("test:testy", {
   hp_max = 1,
   physical = true,
   collide_with_objects = true,
   visual = "cube",
   visual_size = {x = 0.5, y = 0.5},
   textures = {"sides.png", "sides.png", "sides.png", "sides.png", "front.png", "sides.png"},
   velocity = {x=math.random(-1,1), y=0, z=math.random(-1,1)},
   collisionbox = {-0.25, -0.25, -0.25, 0.25, 0.25, 0.25},
   weight = 5, -- ??
   is_visible = true,
   automatic_rotate = true,
   automatic_face_movement_dir = -90, -- set yaw direction in degrees, false to disable
   stepheight = 1.1,
   makes_footstep_sound = true,
   floats = 1,
   view_range = 10,
   speed = 1,
   jump_height = 5,

   set_velocity = function(self, v)
      if not v then v = 0 end
      local yaw = self.object:getyaw()
      self.object:setvelocity({x=math.sin(yaw) * -v, y=self.object:getvelocity().y, z=math.cos(yaw) * v})
   end,

   on_step = function(self, dtime)

      -- every 1 second
      self.timer = (self.timer or 0) + dtime
      if self.timer < 1 then return end
      self.timer = 0

      -- make sure object floats (or not) when in water
      if minetest.get_item_group(minetest.get_node(self.object:getpos()).name, "water") ~= 0 then
         if self.floats == 1 then
            self.object:setacceleration({x = self.object:getacceleration().x, y = 1.5, z = self.object:getacceleration().z})
         end
      else
         self.object:setacceleration({x = self.object:getacceleration().x, y = gravity, z = self.object:getacceleration().z})
      end

      local s, p, dist, nod
      -- find player to follow
      for _,player in pairs(minetest.get_connected_players()) do
         s = self.object:getpos()
         p = player:getpos()

         -- find distance
         dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
         if dist < self.view_range then
            local vec = {x=p.x-s.x, y=p.y-s.y, z=p.z-s.z}
            local yaw = (math.atan(vec.z/vec.x)+math.pi/2)
            if p.x > s.x then
               yaw = yaw + math.pi
            end
            -- face player
            self.object:setyaw(yaw)

            -- find direction and show node facing
            self.direction = {x = math.sin(yaw)*-1, y = 0, z = math.cos(yaw)}
            nod = minetest.get_node_or_nil({x=s.x + self.direction.x,y=s.y+1,z=s.z + self.direction.z})
            print ("facing node", nod.name, dist)

            -- more than 2 nodes ahead then follow, otherwise stop
            if dist > 2 then
               if self.jump_height > 0 and self.object:getvelocity().y == 0 then
                  local v = self.object:getvelocity()
                  v.y = self.jump_height
                  self.object:setvelocity(v)
               end
               self.set_velocity(self, self.speed)
            else
               self.set_velocity(self, 0)
            end
            -- break look after player found
            break
         end
      end

   end,
})

Re: A simple mob

PostPosted: Mon Oct 19, 2015 20:29
by Don
Wow. Thanks!

Re: A simple mob

PostPosted: Mon Oct 19, 2015 23:45
by kaadmy
Don wrote:The mob only needs to chase a player. There is no animation, punch or attack. I don't even want the player to be able to hit the mob.

Ah, Slimes from Terasology?

Re: A simple mob

PostPosted: Tue Oct 20, 2015 15:05
by MineYoshi
NO!
i think is pacman!

Re: A simple mob

PostPosted: Tue Oct 20, 2015 15:45
by Don
It is pacman. I should have it done in the next couple days. I am going to add more games to myboardgames modpack soon.

Re: A simple mob

PostPosted: Tue Oct 20, 2015 16:44
by Ferk
I still think it would make more sense if the ghosts followed a more "square" pattern. You won't be able to use a standard pacman map if the ghosts spawn all in the center and follow this algorithm, or else they will always go in the same direction without spreading out. So you'll need them to spawn in the corners.

Anyway, even if the game turns out different than pacman, it still sounds like fun.

Re: A simple mob

PostPosted: Tue Oct 20, 2015 17:38
by Don
Ferk wrote:I still think it would make more sense if the ghosts followed a more "square" pattern. You won't be able to use a standard pacman map if the ghosts spawn all in the center and follow this algorithm, or else they will always go in the same direction without spreading out. So you'll need them to spawn in the corners.

Anyway, even if the game turns out different than pacman, it still sounds like fun.

I made it look the same as the old pacman but removed a couple blocks from the ghost spawn area.

Re: A simple mob

PostPosted: Sat Oct 31, 2015 18:25
by Gaming Association
so ull b able 2 play it like the original pacman arcade game?

Re: A simple mob

PostPosted: Sat Oct 31, 2015 19:00
by Don
You will have to wait till it is released to see.

Re: A simple mob

PostPosted: Sun Nov 22, 2015 05:56
by Christian9
Don wrote: mob only needs to chase a player. There is no animation, punch or attack. I don't even want the player to be able to hit the mob.


I could do that quickly.give me 3 days and I can get you something just like that

Re: A simple mob

PostPosted: Sun Nov 22, 2015 12:55
by Don
Christian9 wrote:
Don wrote: mob only needs to chase a player. There is no animation, punch or attack. I don't even want the player to be able to hit the mob.


I could do that quickly.give me 3 days and I can get you something just like that

Pacmine is done already. It is in myarcade. The link is in my signature.