2.8 KiB
2.8 KiB
Design: Player AI and Steering System
Goal
Implement autonomous players that move organically on the ice, reacting to the puck and each other, with randomized individual characteristics.
Architecture
The system is based on "Steering Behaviors", where players calculate a desired velocity based on multiple forces, which are then summed and applied to the player's physics.
1. Player Attributes
Each player is assigned a set of attributes that define their physical and mental capabilities.
- Budget: 10 points total.
- Distribution: Each attribute gets a minimum of 1 point; the remaining 6 points are distributed randomly.
- Attributes:
- Speed: Affects
MaxSpeed. Higher speed allows faster movement. - Agility: Affects
MaxForce. Higher agility allows sharper turns and faster acceleration. - Strength: Affects collision impulse. Stronger players push the puck and opponents further.
- Tactics: Affects the weight of steering behaviors (e.g., how effectively they seek the puck).
- Speed: Affects
2. Steering Behaviors
The internal/game/steering.go module will provide the following forces:
- Seek(target): Direct force towards the target (puck).
- Arrive(target): Similar to Seek, but slows down as the player reaches the target to prevent orbiting.
- Avoidance(others): A strong repulsive force to prevent players from overlapping.
- ZoneConstraint(homeZone): A soft force pulling the player back to their half of the ice if they stray too far.
3. File Structure
internal/entities/player.go: DefinesPlayerandAttributesstructs and theNewPlayerconstructor with random attribute logic.internal/game/steering.go: Pure functions for calculating steering vectors.internal/game/world.go:- Manages the list of players.
- Updates player positions using steering forces.
- Handles collisions (Player-Puck, Player-Player) using the
Strengthattribute.
internal/render/renderer.go: Renders players as colored circles (Red/Blue), with size slightly scaled byStrength.
Data Flow
- Per-Tick Update:
- Calculate Steering Forces:
TotalForce = (Seek * Tactics) + Avoidance + ZoneConstraint. - Apply Physics:
Acceleration = TotalForce / Mass(capped byAgility). - Update Velocity:
Velocity += Acceleration(capped bySpeed). - Update Position:
Position += Velocity.
- Calculate Steering Forces:
- Collision Resolution:
- If Player touches Puck: Transfer momentum based on
StrengthandVelocity. - If Player touches Player: Repel based on relative
Strength.
- If Player touches Puck: Transfer momentum based on
- Rendering: Draw players at their current positions.
Edge Cases
- Overlapping: High-priority
Avoidanceforce prevents players from stacking. - Boundary Control: Players are clamped to the ice rink boundaries.
- Damping: A small friction coefficient is applied to velocity to prevent infinite sliding.