项目作者: nezvers

项目描述 :
Collision system that doesn't use collision shapes.
高级语言: Lua
项目地址: git://github.com/nezvers/Defold_true-tile-collision.git
创建时间: 2019-05-27T16:13:15Z
项目社区:https://github.com/nezvers/Defold_true-tile-collision

开源协议:

下载


true-tile-collision

Collision system for Defold that doesn’t use collision shapes.
Check out “example project”.
It’s still in development but it contain everything you need for platformer game and easy to put together top-down game

Easy to use!

Prepare player

  1. require "true_tile_collision.true_tile_collision" -- [1]
  2. function init(self)
  3. msg.post(".", "acquire_input_focus") --Enable inputs for player
  4. init_physics(self, msg.url(), "/background#tilemap", "collision", 16, 2, 5, 0.2) -- [2]
  5. set_hitbox(self, 5, -5, 15, -1) -- [3]
  6. end
  7. function final(self)
  8. msg.post(".", "release_input_focus") --Disable input upon deletion of the object
  9. end
  10. function update(self, dt)
  11. --PHYSICS
  12. physics_update_platformer_block(self, dt) -- [4]
  13. --SPRITE - simple flip
  14. if not self.on_ledge and not self.wallsliding then
  15. if self.xinput==1 then
  16. sprite.set_hflip("#sprite", false)
  17. elseif self.xinput==-1 then
  18. sprite.set_hflip("#sprite", true)
  19. end
  20. end
  21. end
  22. function on_input(self, action_id, action)
  23. -- save all buttons in one variable using bitwise
  24. if action_id == hash("up") then
  25. button_up(self)
  26. elseif action_id == hash("down") then
  27. button_down(self)
  28. elseif action_id == hash("left") then
  29. button_left(self)
  30. elseif action_id == hash("right") then
  31. button_right(self)
  32. elseif action_id == hash("jump") then
  33. button_jump(self)
  34. elseif action_id == hash("action") then --**
  35. button_action(self)
  36. elseif action_id == hash("start") then --**
  37. button_start(self)
  38. end
  39. --** not used for True Tile collision but are there if you want to expand
  40. end
  1. Reference the TrueTileCollision LUA module
  2. Creates all necessary variables inside object. Need to pass (self, URL, collisionTilemap, collisionLayer, tileSize, maxHorizontalSpeed, jumpSpeed, gravity) By default use values pixels per frame (60fps)
  3. Hitbox size (self, right, left, top, bottom) Distance from origin point to sides.
  4. Update physics. Built-in:
    physics_update_platformer_block;
    physics_update_platformer_slopes;
    physics_update_topdown;
    physics_update_walker.

Please look into init_physics() function because there you’ll find needed flags and triggers for your objects (Abilities, States, triggers) to trigger sprite animations/ sounds or create your own AI behaviour.

Prepare tiles

Tiles doesn’t need collision shapes. Collision happens using tile IDs from tilesource.
Suggested method would be to have tilemap with at least 2 layers where one of them is invisible and used for placing solid tiles, or use separate tilemap with dedicated tilesource (if you have more than one tilesources).
If you don’t have dedicated tilesource or IDs doesn’t match the default values, all you need to do is call set_tiles function.

  1. function set_tiles(tile1, tile2, tile3, tile4, tile5, tile6, tile7, tile8)
  2. solid1 = tile1 --solid block
  3. solid2 = tile2 --slope 45 righ
  4. solid3 = tile3 --slope 45 left
  5. solid4 = tile4 --slope 22.5 right 1/2
  6. solid5 = tile5 --slope 22.5 right 2/2
  7. solid6 = tile6 --slope 22.5 left 1/2
  8. solid7 = tile7 --slope 22.5 left 2/2
  9. plat = tile8 --Jumpthrough platform
  10. end

Values are IDs in tilesource used for collision. IDs in tilesource are counted from left to right and top to down, starting from 1.

Create your own physics_update

Since dependancy LUA modules are read only, you can create module that refer to TTC (require “true_tile_collision.true_tile_collision”) and create your own physics updates (for players and enemies) by using modular system from TTC.
For reference you can use TTC built-in physics updates and many physics functions that’s coded inside TTC.

HAPPY GAMEDEV

Cheers, NeZvers