Godot 4 Autoload单例模式最佳实践,包含全局管理、事件总线、信号通信。
The English summary is being prepared. Showing the original description.
Godot 4 种植系统设计,包含植物数据结构、生长阶段、浇水机制、TileMap应用。
The English summary is being prepared. Showing the original description.
Run score Not tested
See how to installReview the instructions the agent will receive before you install.
种植系统采用事件驱动架构,通过全局信号实现地图逻辑与游戏逻辑的解耦,支持多种植物的种植、浇水和生长阶段管理。
┌─────────────────────────────────────────┐
│ UI 层 (FarmUI) │
│ - 工具选择、植物种子选择 │
├─────────────────────────────────────────┤
│ 状态机层 (StateMachine) │
│ - 玩家状态切换、工具使用状态 │
├─────────────────────────────────────────┤
│ 逻辑层 (GlobalPlants) │
│ - 植物数据管理、浇水状态、生命周期 │
├─────────────────────────────────────────┤
│ 地图层 (TileMap) │
│ - GroundMap / EarthMap / PlantsMap │
└─────────────────────────────────────────┘
# globals/global_plants.gd
signal dig_earth(pos: Vector2) # 挖掘土地信号
signal plant_plant(pos: Vector2) # 种植植物信号
signal water_earth(pos: Vector2) # 浇水土地信号
signal update_earth_map_tiles # 更新泥土贴图信号
signal update_plants_map_life # 更新植物生命周期信号
var plants = [
{
"name": "玉米",
"source_id": 0,
"life": [
{
"coord": Vector2i(0, 1),
"require_time": 1
},
{
"coord": Vector2i(1, 1),
"require_time": 1
},
]
},
]
| 字段 | 类型 | 说明 |
|---|---|---|
name | String | 植物名称 |
source_id | int | 瓦片源ID |
life | Array | 生长阶段数组 |
coord | Vector2i | 精灵图坐标 |
require_time | int | 该阶段所需时间(秒) |
var watered_pos: Array[Vector2i] = []
func on_water_earth(pos: Vector2):
var water_pos = earth_map.local_to_map(ground_map.to_local(pos))
if not GlobalPlants.watered_pos.has(water_pos):
GlobalPlants.watered_pos.append(water_pos)
GlobalPlants.update_earth_map_tiles.emit()
PlantMap (场景)
├── GroundMap # 草地层(可挖掘的草地)
├── EarthMap # 泥土层(挖掘后的土地)
└── PlantsMap # 植物层(生长的植物)
func on_dig_earth(pos: Vector2):
var dig_pos = ground_map.local_to_map(ground_map.to_local(pos))
var ground_tile_data = ground_map.get_cell_tile_data(GroundGrassLayer, dig_pos)
if ground_tile_data == null:
return
var can_dig = ground_tile_data.get_custom_data("can_dig")
if !can_dig:
return
var earth_tile_data = earth_map.get_cell_tile_data(EarthDirtLayer, dig_pos)
if earth_tile_data != null:
return
earth_map.set_cells_terrain_connect(EarthDirtLayer, [dig_pos], 0, 0)
func on_plant_plant(pos: Vector2):
var plant_pos = earth_map.local_to_map(ground_map.to_local(pos))
var earth_tile_data = earth_map.get_cell_tile_data(EarthDirtLayer, plant_pos)
var plant_tile_data = plants_map.get_cell_tile_data(PlantsLayer, plant_pos)
if earth_tile_data == null or plant_tile_data != null:
return
var plant = GlobalPlants.plants[GlobalPlants.current_plant]
plants_map.set_cell(PlantsLayer, plant_pos, plant.source_id, plant.life[0].coord)
plant_tile_data = plants_map.get_cell_tile_data(PlantsLayer, plant_pos)
plant_tile_data.set_custom_data("plant", GlobalPlants.current_plant)
plant_tile_data.set_custom_data("life", 0)
plant_tile_data.set_custom_data("require_time", plant.life[0]["require_time"])
func on_update_plants_map_life():
var used_rect = get_used_rect()
for x in used_rect.size.x:
for y in used_rect.size.y:
var coord = Vector2i(x + used_rect.position.x, y + used_rect.position.y)
if not GlobalPlants.watered_pos.has(coord):
continue
var tile_data = get_cell_tile_data(PlantLayer, coord)
if tile_data == null:
continue
var plant_id = tile_data.get_custom_data("plant")
var life = tile_data.get_custom_data("life")
var require_time = tile_data.get_custom_data("require_time")
require_time -= 1
if require_time > 0:
tile_data.set_custom_data("require_time", require_time)
continue
var plant = GlobalPlants.plants[plant_id]
var plant_life = plant.life as Array
life += 1
if life < plant_life.size():
var next_life = plant_life[life]
set_cell(PlantLayer, coord, plant.source_id, next_life.coord)
tile_data = get_cell_tile_data(PlantLayer, coord)
tile_data.set_custom_data("require_time", next_life.require_time)
tile_data.set_custom_data("life", life)
tile_data.set_custom_data("plant", plant_id)
func plant_loop():
await get_tree().create_timer(1).timeout
update_plants_map_life.emit()
plant_loop()
func _tile_data_runtime_update(_layer: int, coords: Vector2i, tile_data: TileData) -> void:
if GlobalPlants.watered_pos.has(coords):
tile_data.modulate = Color(.73, .58, .48, 1.0)
else:
tile_data.modulate = Color.WHITE
| 自定义数据 | 所属层 | 说明 |
|---|---|---|
can_dig | GroundMap | 草地是否可挖掘 |
plant | PlantsMap | 植物类型ID |
life | PlantsMap | 当前生长阶段 |
require_time | PlantsMap | 距离下一阶段所需时间 |
┌─────────────┐ E键 ┌──────────────┐
│ Idle │ ──────────> │ UseTool │
└─────────────┘ └──────┬───────┘
│
┌──────────────┴──────────────┐
│ GlobalTool.current_tool │
└──────────────┬──────────────┘
┌─────────────────┬──────┴───────┬─────────────────┐
"hoe" 挖掘 "kettle" 浇水 "seed" 种植 "axe" 劈砍
│ │ │ │
▼ ▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐
│ dig_earth.emit │ │ water_earth.emit│ │ plant_plant.emit │
└────────┬────────┘ └────────┬────────┘ └──────────┬──────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐
│ 草地 → 泥土 │ │ 记录浇水位置 │ │ 泥土 → 植物 │
└─────────────────┘ └─────────────────┘ └─────────────────────┘
No reviews yet. Tell others what you thought.
No login needed. Share how it worked for you, ask a question, or note where it did not run.
No comments yet. Be the first to write one.
Godot 4 Autoload单例模式最佳实践,包含全局管理、事件总线、信号通信。
The English summary is being prepared. Showing the original description.
Godot 4 Buff系统完整实现,支持状态效果管理、持续时间、循环效果、层叠机制等。使用Resource+Component架构。
The English summary is being prepared. Showing the original description.
Godot 4 建造系统实现,包含GridMap方块管理、第一人称视角、射线交互。
The English summary is being prepared. Showing the original description.
Godot 4 ECS实体组件系统模式,包含组件化设计、数据与逻辑分离、组件通信。
The English summary is being prepared. Showing the original description.
Master Godot 4 GDScript patterns including signals, scenes, state machines, and optimization. Use when building Godot games, implementing game systems, or learning GDScript best practices.
The English summary is being prepared. Showing the original description.
Godot 4 可交互组件设计,包含InteractiveComponent代理组件、NPC对话、区域触发。
The English summary is being prepared. Showing the original description.