Skip to content

Instantly share code, notes, and snippets.

@Jearil
Created November 7, 2023 04:41
Show Gist options
  • Save Jearil/ccd9036c8ff56d11225e663b46826136 to your computer and use it in GitHub Desktop.
Save Jearil/ccd9036c8ff56d11225e663b46826136 to your computer and use it in GitHub Desktop.
HeightAdjuster for Godot
using Godot;
using System;
using System.Collections.Generic;
public partial class HeightAdjuster : Node
{
[Export] public CharacterBody2D Body;
[Export] public int DefaultLayer = 1;
[Export] public int HighLayer = 8;
[Export] public TileMap TileMap;
[Signal]
public delegate void OnBridgeEventHandler();
[Signal]
public delegate void ExitBridgeEventHandler();
private Dictionary<String, int> _layers;
private TileSet _tileSet;
private bool _onBridge;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_layers = new Dictionary<string, int>();
for(int i = 0; i < TileMap.GetLayersCount(); i++)
{
// now we can reference TileMap by layer name.
_layers[TileMap.GetLayerName(i)] = i;
}
_tileSet = TileMap.TileSet;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
var tilemapPosition = TileMap.LocalToMap(Body.GlobalPosition);
var tileData = TileMap.GetCellTileData(_layers["Bridge"], tilemapPosition);
if (tileData == null && _onBridge)
{
// we're not on a bridge layer
_onBridge = false;
EmitSignal(SignalName.ExitBridge);
return;
}
if (tileData == null) return;
var enteringBridge = tileData.GetCustomData("entering_bridge").AsBool();
var onBridge = tileData.GetCustomData("on_bridge").AsBool();
if (enteringBridge && !_onBridge)
{
_onBridge = true;
EmitSignal(SignalName.OnBridge);
} else if (_onBridge && !onBridge)
{
_onBridge = false;
EmitSignal(SignalName.ExitBridge);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment