Author Topic: Cut Vines  (Read 145 times)

gmanplays@gmail.com

  • Newbie
  • *
  • Posts: 15
    • View Profile
Cut Vines
« on: March 04, 2019, 12:48:11 am »
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.

Code: [Select]
-- 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

Max

  • Full Member
  • ***
  • Posts: 201
    • View Profile
Re: Cut Vines
« Reply #1 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?

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: [Select]
[code=lua] your code [/code]
These tags add lines numbers and syntax highlighting for Lua.
« Last Edit: March 04, 2019, 06:28:13 am by Max »

gmanplays@gmail.com

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Cut Vines
« Reply #2 on: March 04, 2019, 06:44:07 am »
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.

Max

  • Full Member
  • ***
  • Posts: 201
    • View Profile
Re: Cut Vines
« Reply #3 on: March 04, 2019, 07:10:59 pm »
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!

llamazing

  • Full Member
  • ***
  • Posts: 168
    • View Profile
Re: Cut Vines
« Reply #4 on: March 05, 2019, 01:26:51 am »
Syntax highlighting is actually broken on the forums right now...

Neovyse

  • Administrator
  • Full Member
  • *****
  • Posts: 141
    • View Profile
Re: Cut Vines
« Reply #5 on: March 20, 2019, 10:03:43 pm »
Quote
Syntax 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!