inital commit of game events

master
j 2024-03-25 23:53:22 -04:00
parent df6b24fc99
commit abcce0985c
1 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,126 @@
import GameState from "./gameState";
class GameEvents {
private events: OregonEvent[];
private totalThreshold: number;
constructor() {
// Initialize the events and their thresholds.
this.events = [
{
name: "Wagon Breakdown",
threshold: 10, // Probability weight
action: (gameState) => {
gameState.totalMileageWholeTrip -= 15;
gameState.amountSpentOnMiscellaneousSupplies -= 8;
return "Wagon breaks down. Lose time and supplies fixing it.";
},
},
{
name: "Ox Injures Leg",
threshold: 15,
action: (gameState) => {
gameState.totalMileageWholeTrip -= 25;
gameState.amountSpentOnAnimals -= 20;
return "Ox injures leg. Slows you down rest of the trip.";
},
},
{
name: "Daughter Breaks Arm",
threshold: 20,
action: (gameState) => {
gameState.totalMileageWholeTrip -= 5 + Math.floor(Math.random() * 4);
gameState.amountSpentOnMiscellaneousSupplies -= 2 + Math.floor(Math.random() * 3);
return "Bad luck--your daughter broke her arm. You had to stop and use supplies to make a sling.";
},
},
{
name: "Ox Wanders Off",
threshold: 25,
action: (gameState) => {
gameState.totalMileageWholeTrip -= 17;
return "Ox wanders off--spend time looking for it.";
},
},
{
name: "Son Gets Lost",
threshold: 30,
action: (gameState) => {
gameState.totalMileageWholeTrip -= 10;
return "Your son gets lost---spend half the day looking for him.";
},
},
{
name: "Unsafe Water",
threshold: 35,
action: (gameState) => {
gameState.totalMileageWholeTrip -= Math.floor(Math.random() * 10) + 2;
return "Unsafe water--lose time looking for clean spring.";
},
},
{
name: "Heavy Rains",
threshold: 40,
action: (gameState) => {
gameState.amountSpentOnFood -= 10;
gameState.amountSpentOnMiscellaneousSupplies -= 15;
gameState.totalMileageWholeTrip -= Math.floor(Math.random() * 10) + 5;
return "Heavy rains---time and supplies lost.";
},
},
{
name: "Fire in Your Wagon",
threshold: 45,
action: (gameState) => {
gameState.amountSpentOnFood -= 40;
gameState.amountSpentOnAmmunition -= 400;
gameState.amountSpentOnMiscellaneousSupplies -= Math.floor(Math.random() * 8) + 3;
gameState.totalMileageWholeTrip -= 15;
return "There was a fire in your wagon—food and supplies damaged!";
},
},
{
name: "Lose Your Way in Heavy Fog",
threshold: 50,
action: (gameState) => {
gameState.totalMileageWholeTrip -= 10 + Math.floor(Math.random() * 5);
return "Lose your way in heavy fog—time is lost.";
},
},
{
name: "Wagon Swamped Fording River",
threshold: 55,
action: (gameState) => {
gameState.amountSpentOnFood -= 30;
gameState.amountSpentOnClothing -= 20;
gameState.totalMileageWholeTrip -= 20 + Math.floor(Math.random() * 20);
return "Wagon gets swamped fording river—lose food and clothes.";
},
}
// Add more events here...
];
this.totalThreshold = this.events.reduce((acc, event) => acc + event.threshold, 0);
}
generateEvent(gameState: GameState): string {
const randomEventNumber = Math.random() * this.totalThreshold;
let cumulativeThreshold = 0;
for (let event of this.events) {
cumulativeThreshold += event.threshold;
if (randomEventNumber < cumulativeThreshold) {
console.log(`Event triggered: ${event.name}`);
return event.action(gameState);
}
}
return "";
}
}
export default GameEvents;
interface OregonEvent {
name: string,
threshold: number,
action: (gameState: GameState) => string
}