Solarus-Games English Forum

Solarus => Development => Topic started by: Starlock on December 23, 2015, 04:51:49 AM

Title: How do you code turret type enemies?
Post by: Starlock on December 23, 2015, 04:51:49 AM
I'm not too sure about how to go about making turrets that remain stationary but continuously fire(I supposed using a timer)

One turret would fire in one direction but does not change direction to follow the hero, one fires in four main directions(N,S,E,W) and another that fires in eight direction (N,NW,NE,W,E,SW,SE,S)
Title: Re: How do you code turret type enemies?
Post by: Christopho on December 23, 2015, 08:58:48 AM
The idea is to have two enemy models: the turret and the fire. I did something similar in Zelda Return of the Hylian Solarus Edition:
https://github.com/christopho/zelda_roth_se/blob/dev/data/enemies/medusa.lua
Title: Re: How do you code turret type enemies?
Post by: wizard_wizzle (aka ZeldaHistorian) on December 23, 2015, 02:16:56 PM
Might also be similar to the Beamos script that Diarandor developed for my game. That thread can be found at http://forum.solarus-games.org/index.php/topic,340.msg1519.html#msg1519 and the code is in his repository which is linked there!
Title: Re: How do you code turret type enemies?
Post by: Starlock on December 24, 2015, 03:40:54 AM
The first turret turned out to be really easy but for the other two (the four direction and eight direction turrets) I want it so that the turrets fire in all of the directions at the same time rather than only one.
Title: Re: How do you code turret type enemies?
Post by: Diarandor on December 24, 2015, 09:53:42 AM
You should be able to code it easily. To shoot at the 8 directions at the same time (and similarly for 4 directions) just use a "for" loop to select each angle:
Code (Lua) Select

-- Function to shoot towards 8 directions.
function entity:shoot()
  for dir = 0,7 do
    local angle = dir*math.pi/4 -- Get angle to shoot.
    --[[ Put your code here to create the bullets and shoot them with that angle. --]]
  end
end
Title: Re: How do you code turret type enemies?
Post by: Starlock on December 26, 2015, 01:20:34 AM
Thanks Diarandor that makes sense I'll try it out soon. Also is there a way to make a turret only activate by a switch like a dart trap? Or would that be a custom entity rather than an enemy?
Title: Re: How do you code turret type enemies?
Post by: wizard_wizzle (aka ZeldaHistorian) on December 26, 2015, 03:49:28 AM
Inactivate the turrets when the map loads and then activate the necessary ones in the sensor:on_activated code.