How do you code turret type enemies?

Started by Starlock, December 23, 2015, 04:51:49 AM

Previous topic - Next topic
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)

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

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!

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.

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
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

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?

Inactivate the turrets when the map loads and then activate the necessary ones in the sensor:on_activated code.