Skip to content

Commit

Permalink
Port Dynamic Tilemap Layers 2D demo to C# Godot mono 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
DevDaddyDownUnder committed Dec 16, 2023
1 parent 4a4b46c commit 1760bd5
Show file tree
Hide file tree
Showing 16 changed files with 428 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mono/dynamic_tilemap_layers/Dynamic TileMap Layers.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Godot.NET.Sdk/4.2.0">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>DynamicTileMapLayers</RootNamespace>
</PropertyGroup>
</Project>
19 changes: 19 additions & 0 deletions mono/dynamic_tilemap_layers/Dynamic TileMap Layers.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dynamic TileMap Layers", "Dynamic TileMap Layers.csproj", "{CBDF7372-1B42-4099-8207-2705FC7B78A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
ExportDebug|Any CPU = ExportDebug|Any CPU
ExportRelease|Any CPU = ExportRelease|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CBDF7372-1B42-4099-8207-2705FC7B78A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBDF7372-1B42-4099-8207-2705FC7B78A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBDF7372-1B42-4099-8207-2705FC7B78A6}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU
{CBDF7372-1B42-4099-8207-2705FC7B78A6}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU
{CBDF7372-1B42-4099-8207-2705FC7B78A6}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU
{CBDF7372-1B42-4099-8207-2705FC7B78A6}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions mono/dynamic_tilemap_layers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Dynamic TileMap Layers

Example of how to make a fake wall using TileMap's
`_tile_data_runtime_update()` method. It shows how
to disable collisions per layer.

Language: C#

Renderer: OpenGL

## Screenshots

![Screenshot](screenshots/fake_wall.png)
Binary file added mono/dynamic_tilemap_layers/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions mono/dynamic_tilemap_layers/icon.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bpov140lx7at3"
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://icon.png"
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
94 changes: 94 additions & 0 deletions mono/dynamic_tilemap_layers/level/TileMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Godot;

public partial class TileMap : Godot.TileMap
{
// You can have multiple layers if you make _secretLayer an array.
private int _secretLayer;
private bool _playerInSecret;
private double _layerAlpha = 1.0;

public TileMap()
{
// Find the secret layer by name.
for (int i = 0; i < GetLayersCount(); i++)
{
if (GetLayerName(i) == "Secret")
{
_secretLayer = i;
}
}
}

public override void _Ready()
{
SetProcess(false);
}

public override void _Process(double delta)
{
if (_playerInSecret)
{
if (_layerAlpha > 0.3)
{
// Animate the layer transparency.
_layerAlpha = Mathf.MoveToward(_layerAlpha, 0.3, delta);
SetLayerModulate(_secretLayer, new Color(1, 1, 1, (float)_layerAlpha));
}
else
{
SetProcess(false);
}
}
else
{
if (_layerAlpha < 1.0)
{
_layerAlpha = Mathf.MoveToward(_layerAlpha, 1.0, delta);
SetLayerModulate(_secretLayer, new Color(1, 1, 1, (float)_layerAlpha));
}
else
{
SetProcess(false);
}
}
}

public override bool _UseTileDataRuntimeUpdate(int layer, Vector2I coords)
{
if (layer == _secretLayer)
{
return true;
}

return false;
}

public override void _TileDataRuntimeUpdate(int layer, Vector2I coords, TileData tileData)
{
// Remove collision for secret layer.
tileData.SetCollisionPolygonsCount(0, 0);
}

private void OnSecretDetectorBodyEntered(Node2D body)
{
// Detect player only.
if (body is not CharacterBody2D)
{
return;
}

_playerInSecret = true;
SetProcess(true);
}

public void OnSecretDetectorBodyExited(Node2D body)
{
if (body is not CharacterBody2D)
{
return;
}

_playerInSecret = false;
SetProcess(true);
}
}
Binary file added mono/dynamic_tilemap_layers/level/obstacle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions mono/dynamic_tilemap_layers/level/obstacle.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://cs8h2qyuakmko"
path="res://.godot/imported/obstacle.png-06287f6b2d26dd03335fd87ab78c2cc2.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://level/obstacle.png"
dest_files=["res://.godot/imported/obstacle.png-06287f6b2d26dd03335fd87ab78c2cc2.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
52 changes: 52 additions & 0 deletions mono/dynamic_tilemap_layers/player/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Godot;

public partial class Player : CharacterBody2D
{
private const float WalkForce = 600;
private const float WalkMaxSpeed = 200;
private const float StopForce = 1300;
private const float JumpSpeed = 200;
private int _gravity;

public override void _Ready()
{
_gravity = (int)ProjectSettings.GetSetting("physics/2d/default_gravity");
}

public override void _PhysicsProcess(double delta)
{
Vector2 velocity = Velocity;

// Horizontal movement code. First, get the player's input.
float walk = WalkForce * Input.GetAxis("move_left", "move_right");

// Slow down the player if they're not trying to move.
if (Mathf.Abs(walk) < WalkForce * 0.2)
{
// The velocity, slowed down a bit, and then reassigned.
velocity.X = (float)Mathf.MoveToward(velocity.X, 0, StopForce * delta);
}
else
{
velocity.X += (float)(walk * delta);
}

// Clamp to the maximum horizontal movement speed.
velocity.X = Mathf.Clamp(velocity.X, -WalkMaxSpeed, WalkMaxSpeed);

// Vertical movement code. Apply gravity.
velocity.Y += (float)(_gravity * delta);

// Check for jumping. is_on_floor() must be called after movement code.
if (IsOnFloor() && Input.IsActionJustPressed("jump"))
{
velocity.Y = -JumpSpeed;
}

// Move based on the velocity and snap to the ground.
// TODO: This information should be set to the CharacterBody properties instead of arguments: snap, Vector2.DOWN, Vector2.UP
// TODO: Rename velocity to linear_velocity in the rest of the script.
Velocity = velocity;
MoveAndSlide();
}
}
Binary file added mono/dynamic_tilemap_layers/player/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions mono/dynamic_tilemap_layers/player/player.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://dfb8rr2fakwgp"
path="res://.godot/imported/player.png-1ad27fc2a62fa126eae918723933dd6f.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://player/player.png"
dest_files=["res://.godot/imported/player.png-1ad27fc2a62fa126eae918723933dd6f.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
17 changes: 17 additions & 0 deletions mono/dynamic_tilemap_layers/player/player.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://player/player.gd" type="Script" id=1]
[ext_resource path="res://player/player.png" type="Texture2D" id=2]

[sub_resource type="RectangleShape2D" id=1]
extents = Vector2(7, 7)

[node name="Player" type="CharacterBody2D"]
script = ExtResource( 1 )

[node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource( 2 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(-0.315559, 0.157784)
shape = SubResource( 1 )
71 changes: 71 additions & 0 deletions mono/dynamic_tilemap_layers/project.godot
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=5

[application]

config/name="Dynamic TileMap Layers with C#"
config/description="Example of how to make a kinematic character controller in 2D using
CharacterBody2D. The character moves around, is affected by moving
platforms, can jump through one-way collision platforms, etc."
run/main_scene="res://world.tscn"
config/features=PackedStringArray("4.2", "C#")
config/icon="res://icon.png"
config/tags=PackedStringArray("2d", "demo")

[display]

window/size/viewport_width=530
window/size/viewport_height=495
window/stretch/mode="canvas_items"
window/stretch/aspect="expand"

[dotnet]

project/assembly_name="Dynamic TileMap Layers"

[input]

jump={
"deadzone": 0.5,
"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":32,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
move_left={
"deadzone": 0.5,
"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
move_right={
"deadzone": 0.5,
"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}

[physics]

common/physics_ticks_per_second=120
2d/default_gravity=500

[rendering]

renderer/rendering_method="gl_compatibility"
environment/defaults/default_clear_color=Color(0.156863, 0.133333, 0.25098, 1)
anti_aliasing/quality/msaa_2d=2
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1760bd5

Please sign in to comment.