Skip to content
This repository has been archived by the owner on Sep 16, 2018. It is now read-only.

Introduced a convenience function for creating patrolling NPCs #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions scripts/functions/npcpatrol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ local mt = {__index=NPCPatrol}
function NPCPatrol:new(name)
local patrol = Patrol:new(name)
setmetatable(patrol, mt)
patrol.blocked_dialouges = {}
patrol.blocked_dialogues = {}
return patrol
end

function NPCPatrol:block(ch, delay)
self.blocked_dialouges[ch] = delay or -1
self.blocked_dialogues[ch] = delay or -1
for _, member in ipairs(self.members) do
member:walk(member:position())
end
Expand All @@ -43,17 +43,17 @@ function NPCPatrol:block(ch, delay)
end

function NPCPatrol:unblock(ch)
self.blocked_dialouges[ch] = nil
self.blocked_dialogues[ch] = nil
end

function NPCPatrol:logic()
local free = true
for ch, timer in pairs(self.blocked_dialouges) do
for ch, timer in pairs(self.blocked_dialogues) do
if timer > 1 or timer < 0 then
free = false
self.blocked_dialouges[ch] = timer - 1
self.blocked_dialogues[ch] = timer - 1
else
self.blocked_dialouges[ch] = nil
self.blocked_dialogues[ch] = nil
end
end

Expand All @@ -62,3 +62,14 @@ function NPCPatrol:logic()
end
end

function NPCPatrol:create_npc(talk_func, update_func)
local blocking_talk_func = function(npc, ch)
self:block(ch)
talk_func(npc, ch)
self:unblock(ch)
end

local npc = create_npc_by_name(self.name, blocking_talk_func, update_func)
self:assign_being(npc)
return npc
end
7 changes: 4 additions & 3 deletions scripts/functions/patrol.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
--[[

Script for grouping beings together and letting them patroul

For creating a patrol choose a name. Then add objects to the map.
The name of the object has to start with the name followed by a space
and then the index number of the waypoint. The type of the object has to
be set to WAYPOINT.

Possible object parameters:
+ stroll -> The radius in pixels that will allow the beings to stroll
is a being out of this radius it will walk to a random
Expand Down Expand Up @@ -58,12 +58,13 @@ function Patrol:new(name)
path[id] = {x=x + w / 2, y=y + h / 2, tolerance=tolerance, stroll=stroll}
end
end

assert(#path >= 1, "Path \"" .. name .. "\" need to have at least one waypoint")

return setmetatable({
position_index = 1,
path = path,
name = name,
members = {}
}, mt)
end
Expand Down
7 changes: 2 additions & 5 deletions scripts/npcs/casern_cellar/prisonerdavid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
local patrol = NPCPatrol:new("Prisoner David")

local function prisoner_talk(npc, ch)
patrol:block(ch)
say("...")
patrol:unblock(ch)
end

local prisoner = create_npc_by_name("Prisoner David", prisoner_talk)

local prisoner = patrol:create_npc(prisoner_talk)
prisoner:set_base_attribute(16, 2)
patrol:assign_being(prisoner)

schedule_every(3, function() patrol:logic() end)
9 changes: 2 additions & 7 deletions scripts/npcs/village/thea.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@
local patrol = NPCPatrol:new("Thea")

local function woman_talk(npc, ch)
patrol:block(ch)

say("I'm really worried about all these boys out there in the forest.")
say("Fighting against the king is a rather serious thing. "
.. "I wonder if they're aware what danger they put themselves into.")
patrol:unblock(ch)
end

local woman = create_npc_by_name("Thea", woman_talk)

local woman = patrol:create_npc(woman_talk)
woman:set_base_attribute(16, 2)
patrol:assign_being(woman)
schedule_every(3, function() patrol:logic() end)

schedule_every(3, function() patrol:logic() end)