Files
apocalypse_infection_zone
Code
64 4,695
apocalypse_infection_zone/behavior_pack/scripts/main.js
import { world, system, Player, Entity } from "@minecraft/server";
import { InfectionSystem } from "./modules/infectionSystem.js";
import { SafeZoneSystem } from "./modules/safeZoneSystem.js";
import { SurvivorSystem } from "./modules/survivorSystem.js";
import { MilitarySystem } from "./modules/militarySystem.js";
import { BuildingSystem } from "./modules/buildingSystem.js";
import { VehicleSystem } from "./modules/vehicleSystem.js";
import { FactionSystem } from "./modules/factionSystem.js";
import { EconomySystem } from "./modules/economySystem.js";
import { ResearchSystem } from "./modules/researchSystem.js";
import { WeatherSystem } from "./modules/weatherSystem.js";
import { EventSystem } from "./modules/eventSystem.js";
import { ReputationSystem } from "./modules/reputationSystem.js";
import { HordeSystem } from "./modules/hordeSystem.js";
import { UIHelper } from "./modules/uiHelper.js";
import CONFIG from "./config.js";
// Global game state
const gameState = {
day: 0,
tick: 0,
isRunning: false,
safeZones: new Map(),
playerData: new Map(),
survivors: new Map(),
factions: new Map(),
buildings: new Map(),
vehicles: new Map(),
hordes: [],
events: [],
reputation: 0,
totalZombieKills: 0,
};
// System instances
const systems = {};
class ApocalypsePlugin {
constructor() {
this.initialized = false;
}
initialize() {
if (this.initialized) return;
// Initialize all systems
systems.infection = new InfectionSystem(gameState, CONFIG);
systems.safeZone = new SafeZoneSystem(gameState, CONFIG);
systems.survivor = new SurvivorSystem(gameState, CONFIG);
systems.military = new MilitarySystem(gameState, CONFIG);
systems.building = new BuildingSystem(gameState, CONFIG);
systems.vehicle = new VehicleSystem(gameState, CONFIG);
systems.faction = new FactionSystem(gameState, CONFIG);
systems.economy = new EconomySystem(gameState, CONFIG);
systems.research = new ResearchSystem(gameState, CONFIG);
systems.weather = new WeatherSystem(gameState, CONFIG);
systems.event = new EventSystem(gameState, CONFIG);
systems.reputation = new ReputationSystem(gameState, CONFIG);
systems.horde = new HordeSystem(gameState, CONFIG);
systems.ui = new UIHelper(gameState, CONFIG);
// Register event handlers
this.registerEventHandlers();
// Start game loop
this.startGameLoop();
this.initialized = true;
console.log("[Apocalypse] Infection Zone initialized successfully!");
}
registerEventHandlers() {
// Player join
world.afterEvents.playerSpawn.subscribe((event) => {
const player = event.player;
if (!gameState.playerData.has(player.id)) {
gameState.playerData.set(player.id, {
safeZoneId: null,
rank: 0,
reputation: 0,
kills: 0,
daysSurvived: 0,
infectionLevel: 0,
isCommander: false,
resources: {
food: CONFIG.safeZone.startingFood,
water: CONFIG.safeZone.startingWater,
materials: CONFIG.safeZone.startingMaterials,
ammo: 20,
fuel: 10,
medicine: 5,
},
});
// Give starter kit
this.giveStarterKit(player);
}
});
// Entity death tracking
world.afterEvents.entityDie.subscribe((event) => {
const { deadEntity, damageSource } = event;
// Track zombie kills
if (deadEntity.typeId.startsWith("apocalypse:")) {
const killer = damageSource?.damagingEntity;
if (killer && killer instanceof Player) {
const data = gameState.playerData.get(killer.id);
if (data) {
data.kills++;
gameState.totalZombieKills++;
systems.military.addExperience(killer, 10);
}
}
}
// Notify survival system
systems.survivor.handleDeath(deadEntity);
});
// Entity hurt (infection spread)
world.afterEvents.entityHurt.subscribe((event) => {
const { hurtEntity, damageSource } = event;
if (hurtEntity instanceof Player) {
const source = damageSource?.damagingEntity;
if (source && source.typeId.startsWith("apocalypse:")) {
systems.infection.tryInfect(hurtEntity, source);
}
}
});
// Player chat commands
world.beforeEvents.chatSend.subscribe((event) => {
const message = event.message;
const player = event.sender;
if (message.startsWith("!")) {
event.cancel = true;
this.handleCommand(player, message.substring(1));
}
});
// Item use
world.afterEvents.itemUse.subscribe((event) => {
const { source, itemStack } = event;
if (itemStack?.typeId.startsWith("apocalypse:")) {
this.handleItemUse(source, itemStack);
}
});
}
giveStarterKit(player) {
const dimension = player.dimension;
try {
player.runCommand(
"give @s apocalypse:survival_knife 1 0"
);
player.runCommand(
"give @s apocalypse:bandage 3 0"
);
player.runCommand(
"give @s apocalypse:water_bottle 2 0"
);
player.runCommand(
"give @s apocalypse:canned_food 3 0"
);
player.runCommand(
"give @s apocalypse:flare_gun 1 0"
);
player.sendMessage("§c[APOCALYPSE]§f You have received your starter kit. Survive!");
} catch (e) {
console.error("[Apocalypse] Error giving starter kit:", e);
}
}
handleCommand(player, command) {
const args = command.split(" ");
const cmd = args[0].toLowerCase();
switch (cmd) {
case "help":
this.showHelp(player);
break;
case "base":
systems.safeZone.openBaseMenu(player);
break;
case "survivors":
systems.survivor.listSurvivors(player);
break;
case "military":
systems.military.openMilitaryMenu(player);
break;
case "build":
systems.building.openBuildMenu(player, args.slice(1).join(" "));
break;
case "research":
systems.research.openResearchMenu(player);
break;
case "status":
this.showStatus(player);