Skip to content

Instantly share code, notes, and snippets.

@lamarmarshall
Created November 13, 2024 02:48
Show Gist options
  • Save lamarmarshall/2f741849f27681ff0a81579e40b67b44 to your computer and use it in GitHub Desktop.
Save lamarmarshall/2f741849f27681ff0a81579e40b67b44 to your computer and use it in GitHub Desktop.
godot 4, lay down different sets of tiles
extends Node2D
@onready var tile_map_layer: TileMapLayer = $TileMapLayer
enum farming_modes{DIRT, SEEDS, WATER, HARVEST}
var farming_mode_states: farming_modes = farming_modes.DIRT
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
managet_tools()
if Input.is_action_just_pressed("interact"):
var mos_pos = get_global_mouse_position()
var tilemap_pos: Vector2i = tile_map_layer.local_to_map(mos_pos)
if farming_mode_states == farming_modes.DIRT:
use_dirt(tilemap_pos)
if farming_mode_states == farming_modes.SEEDS:
use_seeds(tilemap_pos)
func use_seeds(tilemap_pos: Vector2i ) -> void:
if retrieve_custom_data(tilemap_pos, "can_place_seeds"):
tile_map_layer.set_cell(tilemap_pos, 1, Vector2i(0, 0))
func use_dirt(tilemap_pos: Vector2i ) -> void:
tile_map_layer.set_cell(tilemap_pos, 0, Vector2i(1, 0))
func managet_tools() ->void:
if Input.is_action_just_pressed("dirt"):
farming_mode_states = farming_modes.DIRT
if Input.is_action_just_pressed("seeds"):
farming_mode_states = farming_modes.SEEDS
func retrieve_custom_data(tile_map_pos: Vector2i, custom_data_layer: String):
var tile_data: TileData = tile_map_layer.get_cell_tile_data(tile_map_pos)
if tile_data:
return tile_data.get_custom_data(custom_data_layer)
else:
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment