Skip to content

Commit 7a52fbd

Browse files
committed
Platform: Use TileSet for texture
Previously the texture could only be changed by modifying the code. I am working on re-skinning the game to use tiles that are very visually different to the current set (Threadbare's pixel art rather than Kenney's anti-aliased art), and want to allow learners to choose the old style. Since the script works by hardcoding tile coordinates within the texture, I think it makes sense to have it accept a TileSet rather than a texture. But I'm open to persuasion here!
1 parent c4ac46e commit 7a52fbd

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

components/platform/platform.gd

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
class_name Platform
33
extends Node2D
44

5-
const TILE_WIDTH: int = 128
6-
const SPRITE: Texture2D = preload("res://assets/tiles-a.png")
5+
const DEFAULT_TILE_SET = preload("res://spaces/tileset-a.tres")
6+
7+
## Which tileset should be used to draw the platform?
8+
@export var tile_set: TileSet = DEFAULT_TILE_SET:
9+
set = _set_tile_set
710

811
## How many tiles wide is the platform?
912
@export_range(1, 20, 1, "suffix:tiles") var width: int = 3:
@@ -26,6 +29,16 @@ var fall_timer: Timer
2629
@onready var _animation_player := %AnimationPlayer
2730

2831

32+
func _set_tile_set(new_tile_set):
33+
if new_tile_set:
34+
tile_set = new_tile_set
35+
else:
36+
tile_set = DEFAULT_TILE_SET
37+
38+
if is_node_ready():
39+
_recreate_sprites()
40+
41+
2942
func _set_width(new_width):
3043
width = new_width
3144

@@ -44,17 +57,20 @@ func _recreate_sprites():
4457
for c in _sprites.get_children():
4558
c.queue_free()
4659

60+
var tile_width := tile_set.tile_size.x
61+
var sprite: Texture2D = tile_set.get_source(tile_set.get_source_id(0)).texture
62+
4763
_collision_shape.one_way_collision = one_way
48-
_collision_shape.shape.set_size(Vector2(width * TILE_WIDTH, TILE_WIDTH))
64+
_collision_shape.shape.set_size(Vector2(width * tile_width, tile_width))
4965
_area_collision_shape.shape.set_size(
50-
Vector2(width * TILE_WIDTH, _area_collision_shape.shape.size[1])
66+
Vector2(width * tile_width, _area_collision_shape.shape.size[1])
5167
)
5268

53-
var center: float = (width - 1) * TILE_WIDTH / 2.0
69+
var center: float = (width - 1) * tile_width / 2.0
5470

5571
for i in range(0, width):
5672
var new_sprite := Sprite2D.new()
57-
new_sprite.texture = SPRITE
73+
new_sprite.texture = sprite
5874
new_sprite.hframes = 12
5975
new_sprite.vframes = 3
6076
if one_way:
@@ -69,7 +85,7 @@ func _recreate_sprites():
6985
new_sprite.frame_coords = Vector2i(6, 0)
7086
else:
7187
new_sprite.frame_coords = Vector2i(10, 1)
72-
new_sprite.position = Vector2(i * TILE_WIDTH - center, 0)
88+
new_sprite.position = Vector2(i * tile_width - center, 0)
7389
_sprites.add_child(new_sprite)
7490

7591

0 commit comments

Comments
 (0)