From 9644cc4b4acffda7c94a61d2b897d8f61bfee75b Mon Sep 17 00:00:00 2001 From: Ryan Lanny Jenkins Date: Sun, 18 May 2025 23:15:05 -0500 Subject: [PATCH] Add a console. --- src/App.tsx | 2 ++ src/components/Console.tsx | 43 ++++++++++++++++++++++++++++++++++++++ src/store/useGameStore.ts | 32 +++++++++++++++++++++++++++- src/types/index.ts | 7 +++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/components/Console.tsx diff --git a/src/App.tsx b/src/App.tsx index fcec8b3..b8ddbe1 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -11,6 +11,7 @@ import Warehouse from './components/Warehouse' import Market from './components/Market' import { ActionCooldown } from './components/ActionCooldown' import { useSaveSystem } from './store/useSaveSystem' +import { Console } from './components/Console' const appContainerStyle: React.CSSProperties = { maxWidth: '1200px', @@ -51,6 +52,7 @@ function App() { + ) diff --git a/src/components/Console.tsx b/src/components/Console.tsx new file mode 100644 index 0000000..0795146 --- /dev/null +++ b/src/components/Console.tsx @@ -0,0 +1,43 @@ +import React from 'react' +import { useGameStore } from '../store/useGameStore' + +const consoleContainerStyle: React.CSSProperties = { + marginTop: '2rem', + padding: '1rem', + backgroundColor: '#1a1a1a', + borderRadius: '0.5rem', + maxHeight: '200px', + overflowY: 'auto', +} + +const messageStyle: React.CSSProperties = { + color: '#e0e0e0', + fontSize: '0.9rem', + marginBottom: '0.5rem', + fontFamily: 'monospace', +} + +const timestampStyle: React.CSSProperties = { + color: '#666', + marginRight: '0.5rem', +} + +export const Console: React.FC = () => { + const messages = useGameStore((state) => state.consoleMessages) + + return ( +
+ {messages.map((message) => ( +
+ + {new Date(message.timestamp).toLocaleTimeString()} + + {message.text} +
+ ))} + {messages.length === 0 && ( +
No messages yet...
+ )} +
+ ) +} \ No newline at end of file diff --git a/src/store/useGameStore.ts b/src/store/useGameStore.ts index d6fb702..b558c2e 100644 --- a/src/store/useGameStore.ts +++ b/src/store/useGameStore.ts @@ -10,7 +10,7 @@ import { COOLDOWN_DURATION, MARKET_ITEMS, } from '../constants' -import { GameState, PlotState, FieldTool } from '../types' +import { GameState, PlotState } from '../types' const initializeField = (size: number): PlotState[][] => { return Array(size) @@ -53,6 +53,18 @@ export const hasSaveInSlot = (slot: number): boolean => { return !!localStorage.getItem(`${SAVE_SLOT_PREFIX}${slot}`) } +const addConsoleMessage = (state: GameState, text: string) => ( + state.consoleMessages = [ + { + id: Math.random().toString(36).substring(7), + text, + timestamp: Date.now(), + }, + ...state.consoleMessages, + ].slice(0, 50) +) + + export const useGameStore = create< GameState & { plant: (row: number, col: number) => void @@ -78,6 +90,7 @@ export const useGameStore = create< gameSpeed: INITIAL_GAME_SPEED, actionCooldown: 0, tickCount: 0, + consoleMessages: [], assignCrop: (row, col, cropId) => { set( @@ -182,6 +195,14 @@ export const useGameStore = create< const crop = CROPS[plot.current.cropId] const waterNeeded = crop.waterPerTick + // Check if water is running low (less than 25% of what's needed) + if (plot.moisture < waterNeeded * 0.25 && plot.moisture > 0) { + addConsoleMessage( + state, + `Plot (${rowIndex + 1},${colIndex + 1}) ${crop.name} is running low on water!`, + ) + } + // Only grow if fertility is above 0.2 if (plot.moisture >= waterNeeded && plot.fertility >= 0.2) { let growthRate = 1 @@ -196,6 +217,15 @@ export const useGameStore = create< state.plots[rowIndex][colIndex].moisture = plot.moisture - waterNeeded state.plots[rowIndex][colIndex].current.progress = newProgress + + // If the plot just became mature, add a message + if (mature && !plot.current.mature) { + addConsoleMessage( + state, + `Plot (${rowIndex + 1},${colIndex + 1}) ${crop.name} is ready to harvest!`, + ) + } + state.plots[rowIndex][colIndex].current.mature = mature } }) diff --git a/src/types/index.ts b/src/types/index.ts index 7fb72a7..5f0d942 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -30,6 +30,12 @@ export interface InventoryItem { export type FieldTool = 'mark' | 'plant' | 'water' | 'harvest' | 'inspect' +export interface ConsoleMessage { + id: string + text: string + timestamp: number +} + export interface GameState { cash: number inventory: Record @@ -39,6 +45,7 @@ export interface GameState { plots: PlotState[][] gameSpeed: number actionCooldown: number + consoleMessages: ConsoleMessage[] } export interface MarketTransaction {