fix чать инвенторя

This commit is contained in:
jze9
2026-04-16 01:51:46 +05:00
parent 057f5d3d1e
commit 6c3f72385e
35 changed files with 18160 additions and 14313 deletions

107
CLAUDE.md
View File

@@ -4,64 +4,69 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
## Project Overview
This is a Unity game project with two major systems:
1. **Character Controller** - First/third-person movement with animations
2. **Inventory System** - Full inventory with hotbar, drag-drop, crafting, and item generation
Unity game project (no CLI build commands — use the Unity Editor). Two major systems:
1. **Character Controller** — first/third-person movement with animations
2. **Inventory System** hotbar, drag-drop, crafting, world items, save/load
## Controls
- **Movement**: WASD
- **Look**: Mouse
- **Run**: Left Shift
- **Crouch**: C
- **Jump**: Space
- **Toggle Inventory**: Tab
- **Close Inventory**: Escape
- **Hotbar Selection**: 1-9 keys
- **Drop Item**: Q
## Key Files and Structure
### Character Controller (`Assets/charecter-controller/scripts/`)
- `charecter.cs` - Main character controller with movement, jumping, crouching, mouse look
- `PlayerBuilder.cs` - Player setup utility
- `PlayerBuilderTool.cs` - Editor tool for building player objects
### Inventory System (`Assets/Inventory/Scripts/`)
- `InventoryManager.cs` - Core inventory logic (singleton, item operations, save/load)
- `Slot.cs` - Individual inventory slot with stacking logic
- `ItemData.cs` - Item definition (id, name, icon, maxStack, etc.)
- `InventoryEvents.cs` - Centralized event system for inventory changes
- `InventoryItemFactory.cs` - Creates dropped items in the world
- `InventoryDragDrop.cs` - Drag and drop UI handling
- `CraftingManager.cs` - Recipe-based crafting system
- `InventoryUIBuilder.cs` - Runtime UI generation
### Scene
- `Assets/Scenes/SampleScene.unity` - Main test scene
| Action | Key |
|---|---|
| Move | WASD |
| Look | Mouse |
| Run | Left Shift |
| Crouch | C |
| Jump | Space |
| Pickup | E |
| Toggle Inventory | Tab |
| Close Inventory | Escape |
| Hotbar Select | 19 |
| Drop Item | Q |
## Architecture
### Character Controller Design
- Uses Unity's CharacterController component for collision
- Head-body separation: body rotates toward look direction, head can offset within limits
- Animator parameters use `Animator.StringToHash` for performance (Speed, IsMoving, IsCrouching, IsGrounded, Jump)
- Physics-based jumping: `v = sqrt(jumpHeight * -2 * gravity)`
- All movement is framerate-independent with `Time.deltaTime`
### Input Layer — `PlayerInput` (singleton, DontDestroyOnLoad)
All input is centralised in `Assets/charecter-controller/scripts/PlayerInput.cs`. It exposes:
- Continuous properties: `MouseX`, `MouseY`, `Horizontal`, `Vertical`, `Crouch`, `Run`
- C# events: `OnJumpPressed`, `OnPickupPressed`, `OnToggleInventory`, `OnDropPressed`, `OnHotbarSelect`
- `Enabled` flag — set to `false` when the inventory is open to block gameplay input
### Inventory Architecture
- **Singleton pattern**: `InventoryManager.Instance` persists across scenes (DontDestroyOnLoad)
- **Dual storage**: Main inventory (36 slots) + Hotbar (9 slots, indices 0-8)
- **Item lookup**: `_itemDict` dictionary maps item IDs to ItemData
- **Stacking**: Items stack up to `maxStack` defined in ItemData
- **Events**: InventoryEvents provides OnItemAdded, OnItemRemoved, OnInventoryOpened/Closed
Both `charecter.cs` and `InventoryManager.cs` subscribe to `PlayerInput.Instance` events in `Start()`. Never read `Input.*` directly in new code — go through `PlayerInput`.
### Inventory Flow
1. Items added via `AddItem(itemId, amount)` - tries existing stacks first, then empty slots
2. Removed via `RemoveItem(itemId, amount)` - returns true if successful
3. Hotbar selection via `SelectHotbarSlot(index)` - updates visual selection
4. Item dropped via `DropSelectedItem()` - spawns world object via InventoryItemFactory
5. JSON serialization via `ExportToJson()` / `ImportFromJson()`
### Character Controller — `charecter.cs`
- Requires `CharacterController` component
- Head-bone / body separation: body yaw tracks camera yaw when moving; head offset is clamped to `±maxHeadYaw`
- Bone rotations applied in `LateUpdate` via `ApplyRotations()`
- Animator hashes cached as `static readonly int` fields; animator Speed param: 0 = idle, 0.5 = walk, 1 = run
- Item pickup radius-cast in `TryPickupNearbyItem()` (2.5 m sphere, closest `WorldItem` wins)
### Inventory System
**Singleton chain** (all DontDestroyOnLoad, all auto-created if missing):
```
PlayerInput → InventoryManager → InventoryItemFactory
→ InventoryEvents
```
**InventoryManager** (`Assets/Inventory/Scripts/InventoryManager.cs`)
- `slots[]` (36) = main inventory; `hotbarSlots[]` (9, indices 0-8)
- `_itemDict` maps `ItemData.id → ItemData`; rebuilt via `RebuildItemDictionary()` — call this after runtime DB changes
- `AddItem()` fill order: existing main stacks → empty main slots → existing hotbar stacks → empty hotbar slots
- Save/load: JSON to `Application.persistentDataPath/inventory_save.json`; auto-save on `OnApplicationQuit`
**ItemData** — ScriptableObject (`Assets > Create > Inventory > Item`). `id` is auto-assigned as GUID on first `OnEnable`. ItemType enum: `Block, Weapon, Armor, Consumable, Tool, Material`.
**InventoryItemFactory** — creates/drops `WorldItem` GameObjects. Needs an `ItemFactoryConfig` ScriptableObject (`Assets > Create > Inventory > Item Factory Config`) assigned in the Inspector for prefab-based spawning; falls back to a primitive cube if no prefab is configured.
**CraftingManager** — recipe key is sorted comma-joined ingredient IDs (`"id1,id2,id3"`). Recipes can be added at runtime via `AddRecipe()`.
**InventoryUIBuilder** — generates the entire inventory UI programmatically at runtime (Canvas, GridLayoutGroup, Slot components, drag-drop handlers). Assigns `InventoryManager.Instance.slots` and `.hotbarSlots` directly. Do not assign these arrays in the Inspector when using this builder.
**InventorySetup** — convenience MonoBehaviour; add to a "GameManager" object to auto-create all managers and UI on scene start. Has an Editor button in the Inspector. Skips creation if `InventoryManager` already exists.
### Scene Setup
`Assets/Scenes/SampleScene.unity` is the only scene. Typical hierarchy:
- `GameManager` with `InventorySetup` (or manually placed `InventoryManager` + `InventoryUIBuilder`)
- Player with `charecter.cs` + `CharacterController` + Animator + head bone reference
### Meta Files
Unity requires .meta files for all assets - always commit them alongside their assets.
Unity requires `.meta` files for all assets always commit them alongside their assets.