Cut Vines

Started by gmanplays@gmail.com, March 04, 2019, 12:48:11 AM

Previous topic - Next topic
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

March 04, 2019, 06:12:18 AM #1 Last Edit: March 04, 2019, 06:28:13 AM by Max
Hey, gman. From a cursory glance, it looks like vine:overlaps is a function you're trying to call, but it isn't defined anywhere?

Either way, this is kind of an odd way of going about what you're aiming to do, I think, or maybe I just don't understand it.

How I might handle vines I want cut with the sword is to make them an enemy that isn't traversable and doesn't damage the hero, with a life of 1. Or you could make a destructible that can be cut, but can't be lifted. Is there any reason you want to make these a custom entity?

If so, I think there's a better way of writing the collision test. The sprite collision test has a callback function where you can check what the colliding sprite is, see if it's the sword.

Finally, use
[code=lua] your code

[/code]
These tags add lines numbers and syntax highlighting for Lua.

See, that's why I come here. I would've never thought to use another type of enemy because of my association of (practically) inanimate vines with the inanimate hammer pegs. No, there wasn't I was just making an effort. Thanks for giving me the perspective, and I'll make sure to color-code next time.

Yeah, of course! : )

Lots of that kind of different perspectives and ideas just comes from familiarity with the toolset of the API, it'll come in time!

Syntax highlighting is actually broken on the forums right now...

QuoteSyntax highlighting is actually broken on the forums right now...

We are working on the new website, so we cannot fix the forums right now. We'll see after the soon-to-come website release :) I hope you people will like it!

Quote from: Max on March 04, 2019, 06:12:18 AM
Hey, gman. From a cursory glance, it looks like vine:overlaps is a function you're trying to call, but it isn't defined anywhere?
Code (lua) Select
your code
[/code=lua]
These tags add lines numbers and syntax highlighting for Lua.

Just to point this out, it is defined here: http://www.solarus-games.org/doc/1.6/lua_api_entity.html#lua_api_entity_overlaps_rectangle

By the way, that "test function" can and should be improved by using a better collision test, like
Code (lua) Select
entity:overlaps(other_entity, "sprite", entity_sprite, other_entity_sprite)
and then check if the following condition is true to confirm the collision:
Code (lua) Select
if other_entity == hero and other_entity_sprite == hero:get_sprite("sword") then
  return true
end

These conditions can be changed so that other weapons and enemies can destroy vines too, and customize the effect in any case.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."