一个基于六边形棋盘的策略自走战斗 Demo:在格子上摆放建筑、经营布局,靠策略取胜,战斗全自动进行。
A Unity prototype of a hex-grid strategy auto-battler — place structures on a hexagonal board, build up your layout, and let AI-driven units fight it out automatically. You win by strategy and positioning, not by clicking.
This is a personal game-programming prototype exploring the "deploy → strategize → auto-battle" loop popularized by auto-battlers and roguelike tower-defense games:
- Deploy structures (e.g. a cannon tower) onto a hexagonal battlefield.
- Strategize — positioning on the hex grid decides who gets attacked, who gets in range, and how fights resolve.
- Auto-battle — every unit runs its own AI "brain" (a behavior tree) and fights without player micro-management.
The battle flows through distinct phases:
PreBattle → Strategy → Battle → AfterBattle
The interesting part of this project is the from-scratch gameplay framework behind the demo. Everything below is hand-written game code (see Assets/Scripts/):
| System | What it does | Where |
|---|---|---|
| Hexagonal grid & coordinates | Axial hex coordinate system, hex distance, logic↔view projection (with camera yaw), point-in-cell lookup, and a circular (distance-bounded) map. | Battle/BattleField.cs |
| Behavior-Tree AI ("Brain") | A custom behavior tree driving each unit: Action / Controller (Sequence & Selector) / Decorator / Service nodes, a blackboard-style BrainContext, and priority-based interruption so higher-priority behaviors preempt running ones. |
Tools/BehaviorEditor/ · BrainComp.cs |
| Game Ability System (GAS-like) | A node-graph ability pipeline — Find Target → Damage / Effect / Projectile — authored visually and executed at runtime. | Tools/AbilityEditor/ |
| Custom Unity editors | Graph editor windows for authoring behavior trees and abilities as node graphs (built on xNode). | */Editor/ |
| Component-based units | Units composed from AvatarComp, BrainComp, MovementComp; split into CreatureUnit (mobile) and StructureUnit (buildings). |
Battle/Units/ |
| Attribute system | Data-driven stats (MaxHp, Attack, Shield, Speed) with Pure / Value / Rate modifier layering, configured via ScriptableObject. |
Components/UnitAttrCfg.cs |
| A* navigation on hex | A* pathfinding across the hex grid with per-unit walkability. | Systems/NavigationSystem.cs |
| Core systems | Generic Singleton / MonoSingleton, resource loading, camera control, and an event/trigger system. |
Systems/ |
Assets/
├── Scripts/ ← ★ Hand-written game code
│ ├── GameLogic/
│ │ ├── Battle/ Battlefield, hex map, units & components
│ │ ├── Demo/ Demo bootstrap (spawns the sample battle)
│ │ ├── UI/ HUD / head widgets
│ │ └── Utils/ Logging, hex math, debug drawing
│ ├── Systems/ Singletons, navigation, camera, resources, abilities
│ └── Tools/ ★ Behavior-tree & ability node-graph frameworks + editors
├── Resources/ Prefabs, unit/ability configs, sprites
├── Scenes/ SampleScene
├── XLua/ xNode/ TextMesh Pro/ ← third-party libraries (see Credits)
└── Plugins/ native plugins
Requirements: Unity 2022.3 LTS (developed on 2022.3.51f1).
- Clone the repo and open the folder as a project in Unity Hub.
- Open
Assets/Scenes/SampleScene.unity. - Press Play.
DemoInstance.StartNewDemoGame()builds the hex field, places a player cannon at the center, and spawns enemy creatures that path in and are engaged automatically.
The first import will take a while as Unity regenerates the
Library/folder (not committed).
// Assets/Scripts/GameLogic/Demo/DemoInstance.cs
FieldMapData field = new FieldMapData { mapSize = new Vector2Int(6, 6), mapDistance = 6 };
BattleSession.Instance.battleField.InitField(field);
// Player structure at the center of the hex board
CreateUnit("Structure.S_DemoCannon", new UnitData(CampEnum.Player, new HexCoord(0, 0)));
// Enemy creatures spawn around the rim and path inward
CreateUnit("Creature.M_DemoEnemy", new UnitData(CampEnum.Enemy, new HexCoord(3, 3)));This is an early prototype / learning project, not a finished game. Natural next steps:
- Economy & resource loop for the strategy phase
- More structure and creature types + a richer ability library
- Win/lose conditions and wave progression
- Visual polish and UX for building placement
This project bundles third-party libraries so it opens and runs out of the box. All credit to their authors:
- xLua — Lua scripting / hotfix for Unity (Tencent, MIT). Original license kept as
LICENSE.TXT. - xNode — node-graph framework the editors are built on (MIT).
- TextMesh Pro — Unity text rendering.
Original game code under Assets/Scripts/ (excluding the third-party folders above) is authored by me for portfolio purposes.