Author Topic: Enemy that runs away from you  (Read 86 times)

Max

  • Full Member
  • ***
  • Posts: 173
    • View Profile
Enemy that runs away from you
« on: October 24, 2018, 02:02:18 am »
I don't know if this'd be handy for anyone else, but I'd been messing with getting an enemy that runs away from you to be fun, and I think I've finally got it.

This enemy will wader randomly until you get within its detection_distance, then it'll run in the opposite direction of the player. When it hits a wall, it will dash in a random direction for its dash_distance, then go back to running from the player.

I think it's pretty fun to chase, the random dashing when it hits a wall makes it pretty unpredictable and it a pretty good compromise to keep it from getting stuck in corners. There's also an element of danger to trying to corner it, because it very well might just dash through you and damage you. In my testing, it works best and a kind of joke-ier enemy that doesn't do much damage, because whenever it bumps into a wall it careens wildly and is going to smack into the player at some point.

Also, here's a gif of the behavior. It's more fun that it might look, haha:


Code: Lua
  1. local enemy = ...
  2. local game = enemy:get_game()
  3. local map = enemy:get_map()
  4. local hero = map:get_hero()
  5. local sprite
  6. local movement
  7. local dashing
  8. --adjustable stats
  9. local hp = 30
  10. local damage = 1
  11. local detection_distance = 65
  12. local walking_speed = 45
  13. local running_speed = 82
  14. local dash_distance = 80
  15. local dash_speed = 140
  16.  
  17.  
  18. function enemy:on_created()
  19.   sprite = enemy:create_sprite("enemies/" .. enemy:get_breed())
  20.   enemy:set_life(hp)
  21.   enemy:set_damage(damage)
  22. end
  23.  
  24. function enemy:on_movement_changed(movement)
  25.   local direction4 = movement:get_direction4()
  26.   sprite = self:get_sprite()
  27.   sprite:set_direction(direction4)
  28. end
  29.  
  30. function enemy:on_restarted()
  31.   if moement ~= nil then movement:stop() end
  32.   dashing = false
  33.   enemy:go_random()
  34.   enemy:check_hero()
  35. end
  36.  
  37. function enemy:check_hero()
  38.   local dist_hero = enemy:get_distance(hero)
  39.   local _,_,hero_layer = hero:get_position()
  40.   local _,_,enemy_layer = enemy:get_position()
  41.   if enemy_layer == hero_layer and dist_hero < detection_distance and not dashing then
  42.     enemy:run_away()
  43.   elseif not dashing then
  44.     enemy:go_random()
  45.   end
  46.  
  47.   sol.timer.start(enemy, 100, function() enemy:check_hero() end)
  48.  
  49. end
  50.  
  51. function enemy:run_away()
  52.   local angle = enemy:get_angle(hero)
  53.   angle = angle + math.pi
  54.   movement = sol.movement.create("straight")
  55.   movement:set_angle(angle)
  56.   movement:set_speed(running_speed)
  57.   movement:start(enemy)  
  58. end
  59.  
  60. function enemy:go_random()
  61.   movement = sol.movement.create("random_path")
  62.   movement:set_speed(walking_speed)
  63.   movement:start(enemy)
  64. end
  65.  
  66. function enemy:on_obstacle_reached()
  67.   enemy:dash()
  68. end
  69.  
  70. function enemy:dash()
  71.   dashing = true
  72.   movement = sol.movement.create("straight")
  73.   movement:set_angle(math.random(2*math.pi))
  74.   movement:set_speed(dash_speed)
  75.   movement:set_max_distance(dash_distance)
  76.   movement:start(enemy, function()
  77.     dashing = false
  78.     enemy:go_random()
  79.   end)
  80. end
  81.  
  82.  
  83.  
« Last Edit: October 24, 2018, 02:13:03 am by Max »

llamazing

  • Full Member
  • ***
  • Posts: 139
    • View Profile
Re: Enemy that runs away from you
« Reply #1 on: October 24, 2018, 02:59:49 am »
Nice one!

Looks like you have a typo on line 31: "moement"

alexgleason

  • Jr. Member
  • **
  • Posts: 72
  • Vegan on a Desert Island
    • View Profile
    • Vegan on a Desert Island
Re: Enemy that runs away from you
« Reply #2 on: October 24, 2018, 11:10:29 pm »
Haha this is awesome!
RIP Aaron Swartz