Hey again,
So I've been making significant progress in the development of my little project, but I've been caught up in yet another snag that's going to have to be straightened out before I move on. That said, the snag involves an entity script (largely based on/rips off the hammer peg script) which is supposed to first make sure the vine entity (32x32px) is not traversable, then test if the hero is there (I think? It was a part of the hammer peg script), then see if player has obtained a sword (As it's the dungeon item) and then to see if the player is using the sword by testing for the animation "sword1". Then, if the player is calling that animation, then test whether or not the sword is within [arbitrary amount of pixels yet to be established, but 32 for now]. If it is, set the animation to "cut_vines".
When I run the script, I get no exception message, which is awfully confusing seeing as the vines aren't being cut. If you can see how I'm being lua-illiterate, I'd be more than obliged. Thanks much again for your time.
-- Lua script of custom entity vines.
-- This script is executed every time a custom entity with this model is created.
-- Feel free to modify the code below.
-- You can add more events and remove the ones you don't need.
-- See the Solarus Lua API documentation for the full specification
-- of types, events and methods:
-- http://www.solarus-games.org/doc/latest
local vines = ...
local game = vines:get_game()
local map = game:get_map()
-- Event called when the custom entity is initialized.
function vines:on_created()
vines:set_traversable_by(false)
-- Initially, the vines are an obstacle for any entity.
local function test_collision_with_hero_sword(sword, entity)
if entity:get_type() ~= "hero" then
--If it's not the hero, don't heck with it
return false
end
if game:has_item("equipment/sword") ~= true then
-- If he doesn't have the sword, heck him
return false
end
if hero:get_animation() ~= "sword1" then
-- Don't bother testing collisions if the hero is not currently swinging his sword
return false
end
-- The hero is using the sword. Determine the exact point to test.
local hero_direction = entity:get_direction()
local x, y = entity:get_center_position()
if hero_direction == 0 then
-- Right.
x = x + 32
elseif hero_direction == 1 then
-- Up.
y = y - 32
elseif hero_direction == 2 then
-- Left.
x = x - 32
else
-- Down.
y = y + 32
end
-- Test if this point is inside the vines.
return vines:overlaps(x, y)
end
vines:add_collision_test(test_collision_with_hero_sword, function(vines, entity)
--change animation to broken
vines:get_sprite():set_animation("cut_vines")
-- Allow entities to traverse this.
vines:set_traversable_by(true)
-- Disable collision detection, this is no longer needed.
vines:clear_collision_tests()
-- Notify people.
if vines.on_cut ~= nil then
vines:on_cut()
end
end)
end