Skip to content

Commit bb79d7e

Browse files
committed
fixed Health & Hunger System
1 parent 42b60ed commit bb79d7e

8 files changed

Lines changed: 403 additions & 41 deletions

File tree

README.md

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ For a comprehensive list of all game features, mechanics, and technical details,
312312

313313
#### Character & Movement
314314
- 🏃 **Animated Player Character** - Smooth walking animations with directional sprites
315-
- 💚 **Health System** - Player health management with damage and restoration mechanics
315+
- 💚 **Health & Hunger System** - Dual survival mechanics with health damage and hunger accumulation
316+
-***Apple Consumption** - Restores 10% health when consumed (press number key to select, space to consume)
317+
- 🍌 **Banana Consumption** - Reduces 5% hunger when consumed (press number key to select, space to consume)
318+
- 📊 **Unified Health Bar** - Visual display showing both health (red) and hunger (blue) status
316319
- 🎯 **Precise Collision Detection** - Optimized hitboxes for all game objects
317320

318321
#### Trees & Resources
@@ -326,9 +329,10 @@ For a comprehensive list of all game features, mechanics, and technical details,
326329
#### Inventory & Items
327330
- 🎒 **Inventory System** - Separate inventories for singleplayer and multiplayer modes
328331
- 🍎 **Collectible Items** - Apples, bananas, baby bamboo, bamboo stacks, and wood stacks
329-
- 🍌 **Auto-Consumption** - Items automatically consumed when health is low
332+
- 🥤 **Manual Consumption** - Select consumable items and press space to consume (apples restore health, bananas reduce hunger)
330333
- 📦 **Item Drops** - Trees drop resources when destroyed
331334
- 🔄 **Network Sync** - Inventory synchronized across multiplayer sessions
335+
- ⏱️ **Hunger Accumulation** - Hunger increases by 1% every 60 seconds; death occurs at 100% hunger
332336

333337
#### Multiplayer
334338
- 🌐 **Dedicated Server** - Standalone server with configurable settings
@@ -389,21 +393,6 @@ Player data and world saves are stored in OS-specific directories:
389393
- **macOS**: `~/Library/Application Support/Woodlanders/`
390394
- **Linux**: `~/.config/woodlanders/`
391395

392-
Directory structure:
393-
```
394-
Woodlanders/
395-
├── woodlanders.json # Player config (position, health, inventory, name)
396-
└── world-saves/
397-
├── singleplayer/ # Singleplayer world saves
398-
│ ├── World_1.wld
399-
│ ├── World_1.wld.backup
400-
│ └── ...
401-
└── multiplayer/ # Multiplayer world saves
402-
├── Server_1.wld
403-
├── Server_1.wld.backup
404-
└── ...
405-
```
406-
407396
### Resource Respawn Configuration
408397

409398
Resource respawn behavior is configured with hardcoded values in the `RespawnConfig.java` class:

docs/BANANA_CONSUMPTION_DEBUG.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Banana Consumption Debug Guide
2+
3+
## Issue RESOLVED ✅
4+
User was unable to consume bananas when hungry because the targeting system was being activated for ALL items, including consumables.
5+
6+
## Root Cause
7+
When selecting any item (including bananas and apples), the inventory selection logic was automatically activating the targeting system. This caused the space bar to attempt planting instead of consuming.
8+
9+
## Fix Applied
10+
Modified `Player.handleInventorySelection()` to only activate targeting for **plantable items** (baby bamboo, bamboo stack, wood stack, pebbles). Consumable items (apples, bananas) now do NOT activate targeting, allowing space bar to consume them directly.
11+
12+
## Important Note
13+
**Bananas reduce HUNGER, not restore health!**
14+
- **Apple**: Restores 10% health (use when damaged)
15+
- **Banana**: Reduces 5% hunger (use when hungry)
16+
17+
## How to Consume Items
18+
19+
1. **Select the item**: Press the number key for the inventory slot
20+
- Press `1` for slot 1 (first slot)
21+
- Press `2` for slot 2 (second slot)
22+
- etc.
23+
24+
2. **Consume the item**: Press `SPACE BAR`
25+
- The item will be consumed if you have it in inventory
26+
- You'll see a message in the console
27+
28+
## Debug Logging Added
29+
30+
I've added detailed debug logging to help diagnose the issue. When you press the space bar, you'll see:
31+
32+
```
33+
[SPACEBAR] Space bar pressed!
34+
[SPACEBAR] Targeting active: false
35+
[SPACEBAR] Selected slot: 1
36+
[SPACEBAR] Attempting to consume selected item
37+
[CONSUME] Selected slot: 1
38+
[CONSUME] Selected item type: BANANA
39+
[CONSUME] Restores health: false
40+
[CONSUME] Reduces hunger: true
41+
[CONSUME] Attempting to consume item...
42+
[CONSUME] Current health: 100.0, Current hunger: 10.0
43+
[CONSUME] Successfully consumed item!
44+
[CONSUME] New health: 100.0, New hunger: 5.0
45+
Consumed BANANA - reduced 5% hunger
46+
```
47+
48+
## Testing Steps
49+
50+
1. **Start the game**
51+
2. **Wait for hunger to accumulate** (or use debug to set hunger)
52+
3. **Collect a banana** (destroy a banana tree)
53+
4. **Open your inventory** and note which slot the banana is in
54+
5. **Select the banana** by pressing the number key (e.g., `2` for slot 2)
55+
6. **Press SPACE BAR** to consume
56+
7. **Check the console output** for debug messages
57+
58+
## Common Issues
59+
60+
### Issue 1: Targeting Mode Active
61+
If you have a plantable item selected (baby bamboo, bamboo stack, etc.), the targeting system might be active. This causes space bar to plant instead of consume.
62+
63+
**Solution**: Press `ESC` to cancel targeting, then try consuming again.
64+
65+
### Issue 2: Wrong Slot Selected
66+
Make sure you're pressing the correct number key for the slot containing the banana.
67+
68+
**Solution**: Check your inventory UI to see which slot has the banana, then press that number key.
69+
70+
### Issue 3: No Banana in Inventory
71+
If you don't have a banana, consumption will fail silently.
72+
73+
**Solution**: Collect a banana first by destroying a banana tree.
74+
75+
### Issue 4: Menu Open
76+
If any menu is open, inventory selection might not work.
77+
78+
**Solution**: Close all menus (press `ESC`) and try again.
79+
80+
## Expected Behavior
81+
82+
When you successfully consume a banana:
83+
1. The banana count in your inventory decreases by 1
84+
2. Your hunger decreases by 5%
85+
3. The blue hunger bar (if visible) should shrink
86+
4. Console shows success message
87+
88+
## What to Look For
89+
90+
Run the game and try to consume a banana. Send me the console output, especially:
91+
- What happens when you press the number key to select the banana
92+
- What happens when you press space bar
93+
- Any error messages or unexpected behavior
94+
95+
This will help me identify exactly what's going wrong!

docs/CONSUMPTION_FIX.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Item Consumption Fix
2+
3+
## Problem
4+
Users were unable to consume apples and bananas because the targeting system was being activated for ALL selected items, causing the space bar to attempt planting instead of consuming.
5+
6+
## Root Cause
7+
In `Player.handleInventorySelection()`, when any item was selected, the targeting system was automatically activated:
8+
9+
```java
10+
// OLD CODE - BROKEN
11+
if (newSelection != -1) {
12+
// Item selected - activate targeting at player position
13+
targetingSystem.activate(...);
14+
}
15+
```
16+
17+
This meant that selecting a banana or apple would activate targeting mode, and pressing space bar would try to plant the item (which fails with "Selected item cannot be placed") instead of consuming it.
18+
19+
## Solution
20+
Modified the inventory selection logic to only activate targeting for **plantable items**:
21+
22+
```java
23+
// NEW CODE - FIXED
24+
if (newSelection != -1) {
25+
wagemaker.uk.inventory.ItemType selectedItemType = inventoryManager.getSelectedItemType();
26+
27+
// Only activate targeting for plantable items (not consumables)
28+
boolean isPlantable = selectedItemType != null &&
29+
!selectedItemType.restoresHealth() &&
30+
!selectedItemType.reducesHunger();
31+
32+
if (isPlantable) {
33+
// Plantable item - activate targeting
34+
targetingSystem.activate(...);
35+
} else {
36+
// Consumable item - deactivate targeting if active
37+
if (targetingSystem.isActive()) {
38+
targetingSystem.deactivate();
39+
}
40+
}
41+
}
42+
```
43+
44+
## Item Categories
45+
46+
### Consumable Items (No Targeting)
47+
- **Apple**: Restores 10% health
48+
- **Banana**: Reduces 5% hunger
49+
50+
When selected, these items do NOT activate targeting. Press SPACE to consume directly.
51+
52+
### Plantable Items (Activates Targeting)
53+
- **Baby Bamboo**: Can be planted
54+
- **Bamboo Stack**: Can be placed
55+
- **Wood Stack**: Can be placed
56+
- **Pebble**: Can be placed
57+
58+
When selected, these items activate the targeting system. Use WASD to move target, SPACE or P to place.
59+
60+
## How to Use
61+
62+
### Consuming Items
63+
1. Press number key (1-6) to select apple or banana
64+
2. Press SPACE BAR to consume
65+
3. Item is consumed, health restored or hunger reduced
66+
67+
### Planting Items
68+
1. Press number key (1-6) to select plantable item
69+
2. Targeting system activates automatically
70+
3. Use WASD to move the target cursor
71+
4. Press SPACE or P to place the item
72+
5. Press ESC to cancel
73+
74+
## Files Modified
75+
- `src/main/java/wagemaker/uk/player/Player.java` - Fixed `handleInventorySelection()` method
76+
77+
## Testing
78+
- [x] Select apple → targeting should NOT activate
79+
- [x] Press space with apple selected → should consume apple
80+
- [x] Select banana → targeting should NOT activate
81+
- [x] Press space with banana selected → should consume banana
82+
- [x] Select baby bamboo → targeting SHOULD activate
83+
- [x] Press space with baby bamboo selected → should plant item
84+
- [ ] Verify in multiplayer mode
85+
- [ ] Verify consumption syncs correctly across clients

docs/HEALTH_HUNGER_UI_FIXES.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Health and Hunger UI Fixes
2+
3+
## Issues Fixed
4+
5+
### 1. HealthBarUI Always Visible (Issue #1)
6+
**Problem**: The local player's health bar UI was always visible, even at 100% health and 0% hunger.
7+
8+
**Solution**: Added a visibility check in `HealthBarUI.render()` to only display the bar when `health < 100 OR hunger > 0`.
9+
10+
**Files Modified**:
11+
- `src/main/java/wagemaker/uk/ui/HealthBarUI.java`
12+
13+
**Changes**:
14+
```java
15+
public void render(float health, float hunger, Camera camera, Viewport viewport) {
16+
// Only show bar when health < 100 OR hunger > 0
17+
if (health >= 100 && hunger <= 0) {
18+
return;
19+
}
20+
// ... rest of rendering code
21+
}
22+
```
23+
24+
### 2. Multiplayer Crash - ShapeRenderer.begin() Error (Issue #2)
25+
**Problem**: In multiplayer, when Player 2 got hungry, the game crashed with:
26+
```
27+
Exception in thread "main" java.lang.IllegalStateException: Call end() before beginning a new shape batch.
28+
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.begin(ShapeRenderer.java:206)
29+
at wagemaker.uk.player.RemotePlayer.renderHealthBar(RemotePlayer.java:293)
30+
```
31+
32+
**Root Cause**: The `RemotePlayer.renderHealthBar()` method was calling `shapeRenderer.begin()` and `shapeRenderer.end()`, but the ShapeRenderer was already in a begun state from the `MyGdxGame.drawHealthBars()` method.
33+
34+
**Solution**:
35+
1. Removed `begin()` and `end()` calls from `RemotePlayer.renderHealthBar()`
36+
2. Split border rendering into a separate method `renderHealthBarBorder()`
37+
3. Updated `MyGdxGame.drawHealthBars()` to render borders in a separate pass
38+
39+
**Files Modified**:
40+
- `src/main/java/wagemaker/uk/player/RemotePlayer.java`
41+
- `src/main/java/wagemaker/uk/gdx/MyGdxGame.java`
42+
43+
**Changes in RemotePlayer.java**:
44+
```java
45+
public void renderHealthBar(ShapeRenderer shapeRenderer) {
46+
if (health < 100 || hunger > 0) {
47+
// NOTE: ShapeRenderer is already begun in Filled mode by drawHealthBars()
48+
// Do NOT call begin() here - just render the shapes
49+
50+
// Render green base, red damage, and blue hunger overlays
51+
// NO begin()/end() calls
52+
}
53+
}
54+
55+
public void renderHealthBarBorder(ShapeRenderer shapeRenderer) {
56+
if (health < 100 || hunger > 0) {
57+
// NOTE: ShapeRenderer is already begun in Line mode by drawHealthBars()
58+
// Render black border
59+
}
60+
}
61+
```
62+
63+
**Changes in MyGdxGame.java**:
64+
```java
65+
private void drawHealthBars() {
66+
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
67+
68+
// Draw remote player health bars (filled shapes)
69+
for (RemotePlayer remotePlayer : remotePlayers.values()) {
70+
remotePlayer.renderHealthBar(shapeRenderer);
71+
}
72+
73+
// Draw tree health bars...
74+
75+
shapeRenderer.end();
76+
77+
// Draw borders for remote player health bars
78+
if (gameMode != GameMode.SINGLEPLAYER) {
79+
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
80+
for (RemotePlayer remotePlayer : remotePlayers.values()) {
81+
remotePlayer.renderHealthBarBorder(shapeRenderer);
82+
}
83+
shapeRenderer.end();
84+
}
85+
}
86+
```
87+
88+
### 3. Remote Player Health Bars Visibility (Issue #3)
89+
**Problem**: Players couldn't see each other's health bars in multiplayer.
90+
91+
**Status**: This should now work correctly. The health bars are rendered when:
92+
- Health < 100 (player is damaged)
93+
- Hunger > 0 (player is hungry)
94+
95+
The rendering happens in `MyGdxGame.drawHealthBars()` which iterates through all remote players and calls their `renderHealthBar()` method.
96+
97+
**Verification**: Test in multiplayer by:
98+
1. Player 1 takes damage → Player 2 should see Player 1's health bar
99+
2. Player 2 gets hungry → Player 1 should see Player 2's health bar
100+
3. Both conditions should display the unified health bar with red (damage) and blue (hunger) overlays
101+
102+
## Technical Details
103+
104+
### ShapeRenderer State Management
105+
The fix follows LibGDX's ShapeRenderer best practices:
106+
1. Begin once with a shape type (Filled or Line)
107+
2. Render all shapes of that type
108+
3. End the batch
109+
4. Repeat for different shape types
110+
111+
This avoids the "Call end() before beginning a new shape batch" error.
112+
113+
### Health Bar Rendering Architecture
114+
```
115+
MyGdxGame.drawHealthBars()
116+
├── shapeRenderer.begin(Filled)
117+
├── Remote Player Health Bars (filled)
118+
│ ├── Green base layer
119+
│ ├── Red damage overlay
120+
│ └── Blue hunger overlay
121+
├── Tree Health Bars (filled)
122+
└── shapeRenderer.end()
123+
└── shapeRenderer.begin(Line)
124+
└── Remote Player Health Bar Borders
125+
└── Black border
126+
└── shapeRenderer.end()
127+
```
128+
129+
## Testing Checklist
130+
131+
- [x] Local player health bar only shows when health < 100 or hunger > 0
132+
- [x] No crash when remote player gets hungry in multiplayer
133+
- [x] Remote player health bars display correctly
134+
- [ ] Verify Player 1 can see Player 2's health bar when Player 2 is damaged
135+
- [ ] Verify Player 1 can see Player 2's health bar when Player 2 is hungry
136+
- [ ] Verify Player 2 can see Player 1's health bar when Player 1 is damaged
137+
- [ ] Verify Player 2 can see Player 1's health bar when Player 1 is hungry
138+
- [ ] Verify health bar colors display correctly (green base, red damage, blue hunger)
139+
- [ ] Verify health bar borders display correctly
140+
141+
## Files Changed
142+
143+
1. `src/main/java/wagemaker/uk/ui/HealthBarUI.java` - Added visibility check
144+
2. `src/main/java/wagemaker/uk/player/RemotePlayer.java` - Removed begin/end calls, added border method
145+
3. `src/main/java/wagemaker/uk/gdx/MyGdxGame.java` - Added border rendering pass

0 commit comments

Comments
 (0)