-
-
Save Jearil/ccd9036c8ff56d11225e663b46826136 to your computer and use it in GitHub Desktop.
HeightAdjuster for Godot
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
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