Author Topic: How do you code turret type enemies?  (Read 2494 times)

Starlock

  • Full Member
  • ***
  • Posts: 113
    • View Profile
How do you code turret type enemies?
« 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)

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1185
    • View Profile
Re: How do you code turret type enemies?
« Reply #1 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

wizard_wizzle (aka ZeldaHistorian)

  • Sr. Member
  • ****
  • Posts: 263
    • View Profile
Re: How do you code turret type enemies?
« Reply #2 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!

Starlock

  • Full Member
  • ***
  • Posts: 113
    • View Profile
Re: How do you code turret type enemies?
« Reply #3 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.

Diarandor

  • Hero Member
  • *****
  • Posts: 1061
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: How do you code turret type enemies?
« Reply #4 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
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Starlock

  • Full Member
  • ***
  • Posts: 113
    • View Profile
Re: How do you code turret type enemies?
« Reply #5 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?

wizard_wizzle (aka ZeldaHistorian)

  • Sr. Member
  • ****
  • Posts: 263
    • View Profile
Re: How do you code turret type enemies?
« Reply #6 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.