summaryrefslogtreecommitdiff
path: root/client/src/akkamon/render/BattleEngine.ts
blob: aa22e36925f0cae9b061a0d9ec1e560e6a0ab3e4 (plain)
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
import type { BattleEvent, BattleInitEvent, BattleMessage, BattleState, Mon } from '../client/IncomingEvents';
import { baseQueue, queueFromArray } from '../DataWrappers';
import type BattleScene from '../scenes/BattleScene';
import type { WorldScene } from '../scenes/WorldScene';
import { BattleDialogue, BattleOptions } from './battleUI';
import {
    AkkamonEngine
} from './engine/AkkamonEngine';

import { client } from '../../app';

function delay(ms: number) {
    return new Promise(resolve => {
        setTimeout(resolve, ms);
    });
}


export enum BattleEventType {
    INTRODUCTION = "INTRODUCTION"
}

export interface BattleUIEvent {
}

export class InstantUIEvent implements BattleUIEvent {
    constructor(
        public callback: () => void
    ) { }
}

export class DialogueUIEvent implements BattleUIEvent {
    constructor(
        public dialogue: string,
        public callback: () => void
    ) { }
}

export class BattleEngine extends AkkamonEngine {

    public scene?: BattleScene

    private eventsToPlay = baseQueue<BattleEvent>();
    private state: BattleState

    private uiEventTriggers = baseQueue<() => void>();

    private playerName: string
    private opponentName: string

    constructor(
        message: BattleMessage,
    ) {
        super();
        this.state = message.state;
        this.eventsToPlay.pushArray(message.eventsToPlay);

        this.playerName = client.getTrainerID()!.id;
        this.opponentName = this.getOpponentName();

    }

    getOpponentName(): string {
        console.log(this.state.teams);
        for (let key in this.state.teams) {
            if (key !== client.getTrainerID()!.id) {
                return key;
            }
        }
        return '';
    }

    update() {
        if (this.scene && !this.eventsToPlay.isEmpty()) {
            console.log("Playing battle event!");
            console.log(this.eventsToPlay.peek()!);
            this.scene.stopWaitingOnEvent();
            let eventToPlay = this.eventsToPlay.pop()!;
            this[eventToPlay.id](eventToPlay)
        }

        if (!this.scene!.isBusy() && !this.uiEventTriggers.isEmpty()) {
            console.log("Scene is no longer busy! Triggering:");
            console.log(this.uiEventTriggers.peek());
            this.uiEventTriggers.pop()!();
        }
    }

    pushUIEvent(event: () => void) {
        this.uiEventTriggers.push(event);
    }

    [BattleEventType.INTRODUCTION](eventToPlay: BattleEvent) {
        let scene = this.scene!;

        // scene.showPlayerSprites();

        scene.pushEvents(
                         [
                             new DialogueUIEvent(this.opponentName + " wants to fight!", async () => {
                                 scene.removePlayerSprites();
                                 scene.busy = false;
                             }),
                             new DialogueUIEvent(this.opponentName + " sent out " + this.state.teams[this.opponentName].activeMon.name + "!", async () => {
                                 scene.showOpponentMonInterface(this.opponentName, this.state.teams[this.opponentName].activeMon);
                                 scene.busy = false;
                             }),
                             new DialogueUIEvent("Go! " + this.state.teams[this.playerName].activeMon.name + "!", async () => {
                                 scene.showPlayerMonInterface(this.playerName, this.state.teams[this.playerName].activeMon);
                                 scene.busy = false;
                             }),
                         ],
                         () => {
                             scene.clearDialogue();
                             scene.pushMenu(new BattleOptions(
                                                              scene
                                                             ));
                         },
                         new InstantUIEvent(async () => {
                             scene.showPlayerSpritesAndBalls();
                             await delay(1000);
                             scene.busy = false;
                         })
        );


    }

    getActiveMon(): Mon {
        return this.state.teams[this.playerName].activeMon;
    }
}