Created
May 29, 2021 07:00
-
-
Save boruok/eebb946e34966f20143e388f7d40b5ac to your computer and use it in GitHub Desktop.
Script for animating tileset by shifting it frame region
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name AnimatedTile extends Reference | |
var id: int | |
var tilemap: TileMap | |
var frame_region : Rect2 | |
var frame_current : int | |
var frame_count : int | |
# timer | |
var _time : float | |
var _duration : float | |
func _init(id: int, tilemap: TileMap, region: Rect2, frame_count: int, duration: float): | |
self.id = id | |
self.tilemap = tilemap | |
self.frame_count = frame_count | |
self.frame_region = region | |
self._duration = duration | |
add_user_signal("animation_finished") | |
func update(delta: float) -> void: | |
_time += delta | |
if _time >= _duration: | |
tilemap.tile_set.tile_set_region( | |
id, | |
Rect2(Vector2(frame_region.position.x + frame_current * tilemap.cell_size.x, frame_region.position.y), | |
tilemap.cell_size)) | |
_time = 0 | |
frame_current += 1 | |
frame_current %= frame_count | |
emit_signal("animation_finished") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name CTimer extends Reference | |
var _time_left: float | |
var time_wait: float | |
var stopped : bool | |
func update(delta: float) -> void: | |
if stopped : return | |
_time_left += delta | |
if _time_left >= time_wait: | |
_time_left = 0 | |
stopped = true | |
emit_signal("timeout") | |
func start(time: float) -> void: | |
_time_left = 0 | |
time_wait = time | |
stopped = false | |
func _init(): | |
add_user_signal("timeout") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment