Skip to content

Commit

Permalink
update generic_soldier scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
CJBok committed Oct 10, 2017
1 parent b71cbd7 commit 8751a6a
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ local properties = {}
local going_hero = false
local seeing_hero = false
local being_pushed = false
local interrupted = false
local main_sprite = nil
local sword_sprite = nil
local state = "idle"

function enemy:set_properties(prop)

Expand All @@ -51,6 +53,7 @@ function enemy:set_properties(prop)
if properties.hurt_style == nil then
properties.hurt_style = "normal"
end

end

function enemy:on_created()
Expand All @@ -65,17 +68,167 @@ function enemy:on_created()

self:set_invincible_sprite(sword_sprite)
self:set_attack_consequence_sprite(sword_sprite, "sword", "custom")

properties.path = enemy:get_game():get_patrol(enemy)
end


function enemy:on_restarted()
main_sprite:set_animation("idle")
sword_sprite:set_animation("idle")
sol.timer.stop_all(self)
sol.timer.start(self, 1000, function() self:restarted() end)
if state == "idle" and not interrupted then
if properties.path ~= nil then
self:patrol_movement()
end
elseif state == "idle" then
self:random_movement()
end
end


function enemy:on_update()

self:check_state()

end

function enemy:check_state()

local movement = enemy:get_movement()
local animation = enemy:get_sprite():get_animation()
if movement ~= nil and (animation == "walking" or animation == "running") then
self:set_direction(movement:get_direction4())
end

if state == "patrolling" or state == "random" then
if self:can_see_hero() then
state = "provoked"
end
end

if state == "provoked" then
self:set_animation("idle")
self:stop_movement()
state = "provoking"
interrupted = true

sol.timer.start(self, 300, function()
print("timer")
sol.audio.play_sound("hero_seen")
state = "attack"
end)
end

if state == "attack" then
self:attack_hero_movement()
end

if state == "hurt" then
sol.timer.start(self, 300, function()
state = "attack"
end)
end

end


function enemy:patrol_movement()
print("patrol")
state = "patrolling"
self:set_animation("walking")
local movement = sol.movement.create("path")
movement:set_path(properties.path)
movement:set_speed(properties.normal_speed)
movement:set_loop(true)
movement:start(self)
end


function enemy:attack_hero_movement()
print("attack")
state = "attacking"
self:set_animation("running")
local movement = sol.movement.create("target")
movement:set_speed(properties.faster_speed)
movement:start(self)
sol.timer.start(self, 5000, function()
self:searching()
end)
end


function enemy:random_movement()
print("random")
state = "random"
self:set_animation("walking")
local movement = sol.movement.create("random_path")
movement:set_speed(properties.normal_speed)
movement:start(self)
end


function enemy:can_see_hero()
local hero = self:get_map():get_entity("hero")
local _, _, layer = self:get_position()
local _, _, hero_layer = hero:get_position()
local near_hero = layer == hero_layer and self:get_distance(hero) < 100
local facing_hero = main_sprite:get_direction() == self:get_direction4_to(hero)

return near_hero and facing_hero
end


function enemy:searching()
state = "searching"
self:stop_movement()
self:set_animation("idle")
sol.timer.start(self, 2000, function()
state = "idle"
self:restart()
end)
end


function enemy:on_hurt()
state = "hurt"
end

function enemy:on_dying()
self:stop_movement()
state = "died"
end


function enemy:on_custom_attack_received(attack, sprite)
print("hurt")
if attack == "sword" and sprite == sword_sprite then
state = "tapped"
self:set_animation("idle")
sol.audio.play_sound("sword_tapping")
local x, y = self:get_position()
local angle = self:get_angle(self:get_map():get_entity("hero")) + math.pi
local movement = sol.movement.create("straight")
movement:set_speed(128)
movement:set_angle(angle)
movement:set_max_distance(26)
movement:set_smooth(true)
movement:start(self, function()
state = "provoked"
self:restart()
end)
end
end


function enemy:set_direction(direction)
sword_sprite:set_direction(direction)
main_sprite:set_direction(direction)
end

function enemy:set_animation(animation)
sword_sprite:set_animation(animation)
main_sprite:set_animation(animation)
end

--[[
function enemy:restarted()
main_sprite:set_animation("walking")
sword_sprite:set_animation("walking")
Expand All @@ -98,10 +251,7 @@ function enemy:check_hero()
local facing_hero = main_sprite:get_direction() == self:get_direction4_to(hero)
if facing_hero and near_hero and not going_hero then
if properties.play_hero_seen_sound then
--sol.audio.play_sound("hero_seen")
end
self:go_hero()
self:go_hero(false)
elseif not near_hero and going_hero then
self:go_random()
end
Expand Down Expand Up @@ -160,37 +310,39 @@ function enemy:go_random()
going_hero = false
end
function enemy:go_hero()

main_sprite:set_animation("idle")
sword_sprite:set_animation("idle")
self:stop_movement()

seeing_hero = true
function enemy:go_hero(provoked)
if not provoked then
main_sprite:set_animation("idle")
sword_sprite:set_animation("idle")
self:stop_movement()
seeing_hero = true
sol.timer.start(self, 500, function()
sol.audio.play_sound("hero_seen")
main_sprite:set_animation("running")
sword_sprite:set_animation("running")
local movement = sol.movement.create("target")
movement:set_speed(properties.faster_speed)
movement:start(self)
being_pushed = false
seeing_hero = false
going_hero = true
end)
return
end
sol.timer.start(self, 500, function()
sol.audio.play_sound("hero_seen")
main_sprite:set_animation("running")
sword_sprite:set_animation("running")
local movement = sol.movement.create("target")
movement:set_speed(properties.faster_speed)
movement:start(self)
being_pushed = false
seeing_hero = false
going_hero = true

end)
--[[
main_sprite:set_animation("running")
sword_sprite:set_animation("running")
local movement = sol.movement.create("target")
movement:set_speed(properties.faster_speed)
movement:start(self)
being_pushed = false
going_hero = true--]]
going_hero = true
end
--[[
function enemy:on_update()
self:can_see_hero()
print(self:get_game():get_degree(self, self:get_map():get_entity("hero")))
Expand Down
2 changes: 1 addition & 1 deletion data/enemies/sword_soldier_blue.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local enemy = ...

sol.main.load_file("enemies/generic_soldier")(enemy)
sol.main.load_file("enemies/generic/generic_soldier")(enemy)
enemy:set_properties({
main_sprite = "enemies/" .. enemy:get_breed(),
sword_sprite = "enemies/" .. enemy:get_breed() .. "_weapon",
Expand Down
5 changes: 3 additions & 2 deletions data/maps/Castle_Dungeon/B1/C1.dat
Original file line number Diff line number Diff line change
Expand Up @@ -1178,9 +1178,10 @@ jumper{
}

enemy{
name = "patrol1",
layer = 1,
x = 256,
y = 205,
x = 280,
y = 109,
direction = 3,
breed = "sword_soldier_blue",
treasure_name = "small_key",
Expand Down
2 changes: 1 addition & 1 deletion data/maps/Castle_Dungeon/B1/C1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
-- http://www.solarus-games.org/doc/latest

local map = ...
local game = map:get_game()
local game = map:get_game()
9 changes: 9 additions & 0 deletions data/scripts/game/patrols.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local game = ...

function game:get_patrol(npc)

if npc:get_name() == "patrol1" then
return {6,6,6,6,6,6,6,6,6,6,4,4,4,4,4,4,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0}
end
return nil
end
9 changes: 5 additions & 4 deletions data/scripts/initial_game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ local game
function initial_game:initialize_new_savegame(game)

sol.main.load_file("scripts/game/functions")(game)
sol.main.load_file("scripts/game/patrols")(game)
sol.main.load_file("scripts/hero/equipment")(game)

--game:set_starting_location("testmap", "start")
--game:set_starting_location("dungeon_castle_b1", "start")
--game:set_starting_location("lightworld_c2", "start")
--game:set_starting_location("lightworld C3", "link_house")
--game:set_starting_location("Castle_Dungeon/B1/A3", "start")
game:set_starting_location("Castle_Dungeon/B1/C1")

game:set_starting_location("link_house", "start")
--game:set_starting_location("link_house", "start")

game:set_max_life(6)
game:set_life(game:get_max_life())
Expand All @@ -22,8 +23,8 @@ function initial_game:initialize_new_savegame(game)
game:set_max_magic(42)
game:set_magic(0)

--game:set_ability("sword", 1)
--game:set_ability("shield", 1)
game:set_ability("sword", 1)
game:set_ability("shield", 1)

sol.audio.play_music("beginning")
end
Expand Down

0 comments on commit 8751a6a

Please sign in to comment.