Solarus-Games English Forum

Solarus => Development => Topic started by: zutokaza on April 02, 2017, 10:51:06 AM

Title: [Solved]How do I make a circle movement?
Post by: zutokaza on April 02, 2017, 10:51:06 AM
I am having trouble with this because my npc is not making a circle movement. I want the NPC (gerf) to circle around a entity (chest1).

Code ( lua) Select
local circle = sol.movement.create("circle")
circle:set_center(chest1,248,357)
circle:start(gerf)

Title: Re: How do I make a circle movement?
Post by: Diarandor on April 02, 2017, 11:23:31 AM
Did you try to add the remaining properties (radius, angle speed, etc) before starting the movement?
I think the Lua API should give an example for each movement.
Title: Re: How do I make a circle movement?
Post by: zutokaza on April 02, 2017, 07:07:32 PM
It is still just standing there. No error outputs.

Code ( lua) Select
local circle = sol.movement.create("circle")
circle:set_center(chest1,248,357)
circle:set_loop_delay(2000)
circle:set_angle_speed(200)
circle:set_duration(2000)
circle:set_max_rotations(10)
circle:set_initial_angle(360)
circle:set_clockwise(true)
circle:set_radius(360)
circle:set_radius_speed(60)
circle:start(gerf)
Title: Re: How do I make a circle movement?
Post by: Diarandor on April 02, 2017, 07:24:30 PM
This is weird. Does it happen with other types of movements? And are you sure this code is being called? Maybe posting all the code of the script will help.
Title: Re: How do I make a circle movement?
Post by: Diarandor on April 02, 2017, 07:27:46 PM
Also, make sure that your NPC is not initially stuck in a wall, heheh. :P
Title: Re: How do I make a circle movement?
Post by: zutokaza on April 02, 2017, 08:13:24 PM
The NPC works with random_path and jump, so it is not stuck.


Full code:
Code ( lua) Select
local map = ...
local game = map:get_game()
local hero = map:get_hero()

function map:on_started()

  local circle = sol.movement.create("circle")
   circle:set_center(chest1,248,357)
   circle:set_loop_delay(2000)
   circle:set_angle_speed(200)
   circle:set_duration(2000)
   circle:set_max_rotations(10)
   circle:set_initial_angle(360)
   circle:set_clockwise(true)
   circle:set_radius(360)
   circle:set_radius_speed(60)
   circle:start(gerf)

end


The only time anything happens is when I do the following.

Code ( lua) Select
local map = ...
local game = map:get_game()
local hero = map:get_hero()

function map:on_started()
  local circle = sol.movement.create("circle")
   circle:set_radius(360)
   circle:start(gerf)

end
Title: Re: How do I make a circle movement?
Post by: Diarandor on April 02, 2017, 08:32:24 PM
No idea of what is wrong. A workaround is to use a custom entity (I recommend to make it traversable) and code the movement. Idea of how to do it:

local x, y
local r, a, t -- radius, angle, time

and in a timer that increments the angle by t:

local a = a0 + t
x = x0 + r * cos(a)
y = y0 + r * sin(a)

The remaining details are a programming exercise for you :)
Title: Re: How do I make a circle movement?
Post by: zutokaza on April 02, 2017, 08:50:45 PM
So is this a bug?
Title: Re: How do I make a circle movement?
Post by: llamazing on April 03, 2017, 03:00:57 PM
I don't think that script is going to do what you want it to do.

Code (lua) Select
circle:set_center(chest1,248,357)

You are setting the center point of the circle 248 pixels horizontally and 357 pixels vertically away from the chest (i.e. 434 pixels away from the chest in total).

With a radius of 360 pixels, your NPC will not be circling the chest. My guess is the reason the script is not working is because the path you've given the NPC takes it beyond the borders of the map? Hard to say without actually seeing your map.

Have you tried just this?
Code (lua) Select
circle:set_center(chest1)
Title: Re: How do I make a circle movement?
Post by: Diarandor on April 03, 2017, 06:56:13 PM
I agree with llamazing :D
Title: Re: How do I make a circle movement?
Post by: zutokaza on April 03, 2017, 07:11:41 PM
llamazing is correct. I did that and changed my radius to 20. Is it possible to have the sprite use all 4 facing directions when going around an entity or would it be better to loop with the path movement?

Code ( lua) Select
local circle = sol.movement.create("circle")
circle:set_center(chest1)
circle:set_loop_delay(1000)
circle:set_angle_speed(100)
circle:set_duration(100)
circle:set_max_rotations(100)
circle:set_initial_angle(100)
circle:set_clockwise(true)
circle:set_radius(20)
circle:set_radius_speed(1)
circle:set_ignore_obstacles(true)
circle:start(gerf)
Title: Re: How do I make a circle movement?
Post by: Diarandor on April 03, 2017, 07:21:28 PM
You can do it with both types of movements. Try to change the direction of the NPC if necessary, when some of these events are called: "entity:on_movement_changed(movement)" or "entity:on_position_changed(x, y, layer)".
Title: Re: How do I make a circle movement?
Post by: zutokaza on April 03, 2017, 09:33:45 PM
I tried the following, but it did nothing. I normally would do this for an enemy to use the 4 way directions.

Code ( lua) Select
function gerf:on_movement_changed()

  sprite:set_direction(movement:get_direction4())
end