File size: 6,376 Bytes
076c3cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
 * Adaptive Tactics - Content Loader
 * Fetches and parses all JSON content files
 */

import { Weapon, Unit, MapData } from './models.js';

/**
 * Fetch and parse a JSON file
 */
async function fetchJSON(path) {
    console.log(`Loading: ${path}`);
    try {
        const response = await fetch(path);
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        const data = await response.json();
        console.log(`Loaded: ${path}`);
        return data;
    } catch (error) {
        console.error(`Failed to load ${path}:`, error);
        throw error;
    }
}

/**
 * Load all game content
 */
export async function loadAllContent() {
    const content = {
        weapons: {},
        playerUnits: [],
        ephraimForms: {},
        enemyTemplates: {},
        map: null,
        majorPackages: {},
        minorModifiers: {},
        transitionLines: {},
        gameRules: {}
    };

    // Load all content files in parallel
    const [
        weaponsData,
        playerUnitsData,
        ephraimFormsData,
        enemyTemplatesData,
        mapData,
        majorPackagesData,
        minorModifiersData,
        transitionLinesData,
        gameRulesData
    ] = await Promise.all([
        fetchJSON('content/items/weapons.json'),
        fetchJSON('content/units/player_units.json'),
        fetchJSON('content/units/ephraim_forms.json'),
        fetchJSON('content/units/forced_servitude_templates.json'),
        fetchJSON('content/maps/fortress_map_01.json'),
        fetchJSON('content/adaptation/major_packages.json'),
        fetchJSON('content/adaptation/minor_modifiers.json'),
        fetchJSON('content/text/cycle_transition_lines.json'),
        fetchJSON('content/balance/game_rules.json')
    ]);

    // Process weapons
    for (const [id, data] of Object.entries(weaponsData.weapons)) {
        content.weapons[id] = new Weapon(data);
    }

    // Process player units
    for (const data of playerUnitsData.player_units) {
        // Build weapon references for the unit
        const unitWeapons = {};
        for (const weaponId of data.weapons) {
            if (content.weapons[weaponId]) {
                unitWeapons[weaponId] = content.weapons[weaponId];
            }
        }
        content.playerUnits.push({
            template: data,
            weapons: unitWeapons
        });
    }

    // Process Ephraim forms
    content.ephraimForms = ephraimFormsData.ephraim_forms;

    // Process enemy templates
    content.enemyTemplates = enemyTemplatesData.enemy_templates;

    // Process map
    content.map = new MapData(mapData);

    // Process adaptation packages
    content.majorPackages = majorPackagesData.major_packages;
    content.minorModifiers = minorModifiersData.minor_modifiers;

    // Process transition lines
    content.transitionLines = transitionLinesData.transition_lines;

    // Process game rules
    content.gameRules = gameRulesData.rules;

    return content;
}

/**
 * Create a player unit instance from template
 */
export function createPlayerUnit(template, weapons, playerName = null) {
    const data = { ...template };
    data.faction = 'player';

    // Set player name for Oathbearer
    if (data.id === 'oathbearer' && playerName) {
        data.name = playerName;
    }

    return new Unit(data, weapons);
}

/**
 * Create an enemy unit instance from template
 */
export function createEnemyUnit(templateId, templates, weapons, cycle, weaponId = null) {
    const template = templates[templateId];
    if (!template) {
        console.error(`Unknown enemy template: ${templateId}`);
        return null;
    }

    // Determine scaling band
    let scalingKey;
    if (templateId === 'dark_mount') {
        if (cycle === 2) scalingKey = 'cycle_2';
        else if (cycle <= 4) scalingKey = 'cycles_3_4';
        else if (cycle <= 6) scalingKey = 'cycles_5_6';
        else scalingKey = 'cycles_7_plus';
    } else {
        if (cycle <= 2) scalingKey = 'cycles_1_2';
        else if (cycle <= 4) scalingKey = 'cycles_3_4';
        else if (cycle <= 6) scalingKey = 'cycles_5_6';
        else scalingKey = 'cycles_7_plus';
    }

    const stats = template.scaling[scalingKey];
    if (!stats) {
        console.error(`No scaling data for ${templateId} at ${scalingKey}`);
        return null;
    }

    // Determine weapon
    const equippedWeapon = weaponId || template.weapons[0];

    // Build unit data
    const unitData = {
        id: `${templateId}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
        name: template.name,
        title: '',
        sprite: template.sprite,
        faction: 'enemy',
        stats: {
            hp: stats.hp,
            maxHp: stats.hp,
            power: stats.power,
            skill: stats.skill,
            speed: stats.speed,
            def: stats.def,
            res: stats.res,
            mov: stats.mov
        },
        weapons: [equippedWeapon],
        equippedWeapon: equippedWeapon
    };

    // Build weapon references
    const unitWeapons = {};
    unitWeapons[equippedWeapon] = weapons[equippedWeapon];

    return new Unit(unitData, unitWeapons);
}

/**
 * Create Ephraim unit instance
 */
export function createEphraim(forms, weapons, formId) {
    const form = forms[formId];
    if (!form) {
        console.error(`Unknown Ephraim form: ${formId}`);
        return null;
    }

    const unitData = {
        id: 'ephraim',
        name: form.name,
        title: form.title,
        sprite: form.sprite,
        portrait: form.portrait,
        faction: 'enemy',
        stats: {
            hp: form.stats.hp,
            maxHp: form.stats.maxHp,
            power: form.stats.power,
            skill: form.stats.skill,
            speed: form.stats.speed,
            def: form.stats.def,
            res: form.stats.res,
            mov: form.stats.mov
        },
        weapons: form.weapons,
        equippedWeapon: form.equippedWeapon,
        behavior: form.behavior
    };

    // Build weapon references
    const unitWeapons = {};
    for (const weaponId of form.weapons) {
        if (weapons[weaponId]) {
            unitWeapons[weaponId] = weapons[weaponId];
        }
    }

    const unit = new Unit(unitData, unitWeapons);
    unit.behavior = form.behavior;
    unit.portrait = form.portrait;

    return unit;
}