Skip to content

Commit 5f8a917

Browse files
committed
Add dynamic TileMap layer demo
1 parent 1113baf commit 5f8a917

File tree

14 files changed

+336
-0
lines changed

14 files changed

+336
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Dynamic TileMap Layers
2+
3+
Example of how to make a fake wall using TileMap's
4+
`_tile_data_runtime_update()` method. It shows how
5+
to disable collisions per layer.
6+
7+
Language: GDScript
8+
9+
Renderer: OpenGL
10+
11+
## Screenshots
12+
13+
![Screenshot](screenshots/fake_wall.png)

2d/dynamic_tilemap_layers/icon.png

4.28 KB
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://bpov140lx7at3"
6+
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://icon.png"
14+
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
extends TileMap
2+
3+
var secret_layer: int # You can have multiple layers if you make this an array.
4+
var player_in_secret: bool
5+
var layer_alpha := 1.0
6+
7+
func _init() -> void:
8+
for i in get_layers_count(): # Find the secret layer by name.
9+
if get_layer_name(i) == "Secret":
10+
secret_layer = i
11+
12+
func _ready() -> void:
13+
set_process(false)
14+
15+
func _process(delta: float) -> void:
16+
if player_in_secret:
17+
if layer_alpha > 0.3:
18+
layer_alpha = move_toward(layer_alpha, 0.3, delta) # Animate the layer transparency.
19+
set_layer_modulate(secret_layer, Color(1, 1, 1, layer_alpha))
20+
else:
21+
set_process(false)
22+
else:
23+
if layer_alpha < 1.0:
24+
layer_alpha = move_toward(layer_alpha, 1.0, delta)
25+
set_layer_modulate(secret_layer, Color(1, 1, 1, layer_alpha))
26+
else:
27+
set_process(false)
28+
29+
func _use_tile_data_runtime_update(layer: int, _coords: Vector2i) -> bool:
30+
if layer == secret_layer:
31+
return true
32+
return false
33+
34+
func _tile_data_runtime_update(_layer: int, _coords: Vector2i, tile_data: TileData) -> void:
35+
tile_data.set_collision_polygons_count(0, 0) # Remove collision for secret layer.
36+
37+
func _on_secret_detector_body_entered(body: Node2D) -> void:
38+
if not body is CharacterBody2D: # Detect player only.
39+
return
40+
41+
player_in_secret = true
42+
set_process(true)
43+
44+
func _on_secret_detector_body_exited(body: Node2D) -> void:
45+
if not body is CharacterBody2D:
46+
return
47+
48+
player_in_secret = false
49+
set_process(true)
1.81 KB
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://cs8h2qyuakmko"
6+
path="res://.godot/imported/obstacle.png-06287f6b2d26dd03335fd87ab78c2cc2.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://level/obstacle.png"
14+
dest_files=["res://.godot/imported/obstacle.png-06287f6b2d26dd03335fd87ab78c2cc2.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
extends CharacterBody2D
2+
3+
const WALK_FORCE = 600
4+
const WALK_MAX_SPEED = 200
5+
const STOP_FORCE = 1300
6+
const JUMP_SPEED = 200
7+
8+
@onready var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
9+
10+
func _physics_process(delta):
11+
# Horizontal movement code. First, get the player's input.
12+
var walk = WALK_FORCE * (Input.get_axis(&"move_left", &"move_right"))
13+
# Slow down the player if they're not trying to move.
14+
if abs(walk) < WALK_FORCE * 0.2:
15+
# The velocity, slowed down a bit, and then reassigned.
16+
velocity.x = move_toward(velocity.x, 0, STOP_FORCE * delta)
17+
else:
18+
velocity.x += walk * delta
19+
# Clamp to the maximum horizontal movement speed.
20+
velocity.x = clamp(velocity.x, -WALK_MAX_SPEED, WALK_MAX_SPEED)
21+
22+
# Vertical movement code. Apply gravity.
23+
velocity.y += gravity * delta
24+
25+
# Move based on the velocity and snap to the ground.
26+
# TODO: This information should be set to the CharacterBody properties instead of arguments: snap, Vector2.DOWN, Vector2.UP
27+
# TODO: Rename velocity to linear_velocity in the rest of the script.
28+
move_and_slide()
29+
30+
# Check for jumping. is_on_floor() must be called after movement code.
31+
if is_on_floor() and Input.is_action_just_pressed(&"jump"):
32+
velocity.y = -JUMP_SPEED
502 Bytes
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://dfb8rr2fakwgp"
6+
path="res://.godot/imported/player.png-1ad27fc2a62fa126eae918723933dd6f.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://player/player.png"
14+
dest_files=["res://.godot/imported/player.png-1ad27fc2a62fa126eae918723933dd6f.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[gd_scene load_steps=4 format=2]
2+
3+
[ext_resource path="res://player/player.gd" type="Script" id=1]
4+
[ext_resource path="res://player/player.png" type="Texture2D" id=2]
5+
6+
[sub_resource type="RectangleShape2D" id=1]
7+
extents = Vector2(7, 7)
8+
9+
[node name="Player" type="CharacterBody2D"]
10+
script = ExtResource( 1 )
11+
12+
[node name="Sprite2D" type="Sprite2D" parent="."]
13+
texture = ExtResource( 2 )
14+
15+
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
16+
position = Vector2(-0.315559, 0.157784)
17+
shape = SubResource( 1 )

0 commit comments

Comments
 (0)