diff options
Diffstat (limited to 'data')
-rw-r--r-- | data/scripts/libtmw.lua | 16 | ||||
-rw-r--r-- | data/test.lua | 41 |
2 files changed, 37 insertions, 20 deletions
diff --git a/data/scripts/libtmw.lua b/data/scripts/libtmw.lua index 662488e3..d55d8b41 100644 --- a/data/scripts/libtmw.lua +++ b/data/scripts/libtmw.lua @@ -4,7 +4,8 @@ -- Table that associates to each NPC pointer the handler function that is -- called when a player starts talking to an NPC. -local npcs = {} +local npc_talk_functs = {} +local npc_update_functs = {} -- Table that associates to each Character pointer its state with respect to -- NPCs (only one at a time). A state is an array with four fields: @@ -24,9 +25,10 @@ local timer -- Creates an NPC and associates the given handler. -- Note: Cannot be called until map initialization has started. -function create_npc(id, x, y, handler) +function create_npc(id, x, y, talkfunct, updatefunct) local npc = tmw.npc_create(id, x, y) - npcs[npc] = handler + if talkfunct then npc_talk_functs[npc] = talkfunct end + if updatefunct then npc_update_functs[npc] = updatefunct end end -- Waits for the player to acknowledge the previous message, if any. @@ -121,7 +123,7 @@ end -- Creates a coroutine based on the registered NPC handler. function npc_start(npc, ch) states[ch] = nil - local h = npcs[npc] + local h = npc_talk_functs[npc] if not h then return end local w = { npc, coroutine.create(h) } if process_npc(w, npc, ch) then @@ -177,6 +179,8 @@ end -- Called by the game every tick for each NPC. function npc_update(npc) + local h = npc_update_functs[npc]; + if h then h(npc) end; end -- Called by the game every tick. @@ -214,7 +218,7 @@ end function create_npc_delayed(id, x, y) -- Bind the name to a local variable first, as it will be reused. local h = npc_handler - atinit(function() create_npc(id, x, y, h) end) + atinit(function() create_npc(id, x, y, h, nil) end) npc_handler = nil end @@ -235,4 +239,4 @@ end tmw.chr_money = function(ch) return tmw.chr_inv_count(ch, 0) -end +end
\ No newline at end of file diff --git a/data/test.lua b/data/test.lua index d4a161d1..3bc630fb 100644 --- a/data/test.lua +++ b/data/test.lua @@ -3,13 +3,13 @@ -------------- atinit(function() - create_npc(110, 50 * 32 + 16, 19 * 32 + 16, my_npc1) - create_npc(108, 51 * 32 + 16, 25 * 32 + 16, my_npc4) - create_npc(126, 45 * 32 + 16, 25 * 32 + 16, my_npc5) - create_npc(122, 58 * 32 + 16, 15 * 32 + 16, my_npc6) + create_npc(110, 50 * 32 + 16, 19 * 32 + 16, npc1_talk, nil) + create_npc(108, 51 * 32 + 16, 25 * 32 + 16, npc4_talk, nil) + create_npc(126, 45 * 32 + 16, 25 * 32 + 16, npc5_talk, npc5_update) + create_npc(122, 58 * 32 + 16, 15 * 32 + 16, npc6_talk, nil) end) -function my_npc1(npc, ch) +function npc1_talk(npc, ch) do_message(npc, ch, "Hello! I am the testing NPC.") do_message(npc, ch, "This message is just here for testing intertwined connections.") do_message(npc, ch, "What do you want?") @@ -49,7 +49,7 @@ function my_npc1(npc, ch) end end -function my_npc4(npc, ch) +function npc4_talk(npc, ch) do_message(npc, ch, "Where do you want to go?") local v = do_choice(npc, ch, "Map 1", "Map 3") if v >= 1 and v <= 2 then @@ -65,27 +65,40 @@ function my_npc4(npc, ch) end end -function my_npc5(npc, ch) +function npc5_talk(npc, ch) do_message(npc, ch, "I am the spider tamer. Do you want me to spawn some spiders?") local answer = do_choice(npc, ch, "Yes", "No"); if answer == 1 then - tmw.monster_create(1012, 44 * 32 + 16, 24 * 32 + 16) - tmw.monster_create(1012, 44 * 32 + 16, 26 * 32 + 16) - tmw.monster_create(1012, 46 * 32 + 16, 24 * 32 + 16) - tmw.monster_create(1012, 46 * 32 + 16, 26 * 32 + 16) + local x = tmw.posX(npc) + local y = tmw.posY(npc) + tmw.monster_create(1012, x + 32, y + 32) + tmw.monster_create(1012, x - 32, y + 32) + tmw.monster_create(1012, x + 32, y - 32) + tmw.monster_create(1012, x - 32, y - 32) end end +local spidertamer_timer = 0 + +function npc5_update(npc) + spidertamer_timer = spidertamer_timer + 1 + if spidertamer_timer == 50 then + spidertamer_timer = 0 + local x = math.random(-64, 64) + tmw.posX(npc) + local y = math.random(-64, 64) + tmw.posY(npc) + tmw.being_walk(npc, x, y, 500) + end +end local guard_position = 1 -function my_npc6(npc, ch) - +function npc6_talk(npc, ch) + if guard_position == 1 then tmw.being_walk(npc, 61 * 32 + 16, 15 * 32 + 16, 400) guard_position = 2 else tmw.being_walk(npc, 55 * 32 + 16, 15 * 32 + 16, 400) guard_position = 1 - end + end end
\ No newline at end of file |