Welcome to mirror list, hosted at ThFree Co, Russian Federation.

scriptablecampaign.cpp « Game « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 41677c9051d22b628ab18bb55e3524d5f193ab91 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//-----------------------------------------------------------------------------
//           Name: scriptablecampaign.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "scriptablecampaign.h"

#include <Scripting/angelscript/ascontext.h>
#include <Scripting/angelscript/asfuncs.h>

#include <Game/levelset_script.h>
#include <Game/savefile.h>
#include <Game/savefile_script.h>
#include <Game/level.h>

#include <Internal/levelxml_script.h>
#include <Internal/filesystem.h>
#include <Internal/modloading.h>
#include <Internal/common.h>
#include <Internal/profiler.h>

#include <Graphics/text.h>
#include <Utility/assert.h>
#include <Main/engine.h>

ScriptableCampaign::ScriptableCampaign() {

}

ScriptableCampaign::~ScriptableCampaign() {

}

static void ASSendLevelMessage( const std::string& msg ) {
    SceneGraph* s = Engine::Instance()->GetSceneGraph();

    if(s) {
        s->level->Message(msg);
    } else {
        LOGW << "Sending message from campaign to level, but there is no scenegraph, going to the void, it was: " << msg << std::endl;
    }
}

void ScriptableCampaign::Initialize(Path script_path, std::string _campaign_id) {
    campaign_id = _campaign_id;

    ASData as_data;

    as_context = new ASContext("scriptable_campaign", as_data);

    as_context->RegisterGlobalFunction("void SendLevelMessage(const string& in msg)",                asFUNCTION(ASSendLevelMessage),                  asCALL_CDECL);

    AttachLevelSet(as_context);
    AttachLevelXML(as_context);
    AttachUIQueries(as_context);
    AttachTokenIterator(as_context);
    AttachSimpleFile(as_context);
    AttachInterlevelData(as_context);
    AttachEngine(as_context);
    AttachOnline(as_context);

    AttachSaveFile( as_context, &Engine::Instance()->save_file_ );

    as_funcs.init                   = as_context->RegisterExpectedFunction("void Init()", false);
    as_funcs.receive_message        = as_context->RegisterExpectedFunction("void ReceiveMessage(string)", false);
    as_funcs.set_window_dimensions  = as_context->RegisterExpectedFunction("void SetWindowDimensions(int width, int height)", false);

    as_funcs.update                 = as_context->RegisterExpectedFunction("void Update()", true);
    as_funcs.dispose                = as_context->RegisterExpectedFunction("void Dispose()", true);
    as_funcs.enter_campaign         = as_context->RegisterExpectedFunction("void EnterCampaign()", true);
    as_funcs.enter_level            = as_context->RegisterExpectedFunction("void EnterLevel()", true);
    as_funcs.leave_campaign         = as_context->RegisterExpectedFunction("void LeaveCampaign()", true);
    as_funcs.leave_level            = as_context->RegisterExpectedFunction("void LeaveLevel()", true);

    char path[kPathSize];
    FormatString(path, kPathSize, "%sasscriptable_campaign_docs.h", GetWritePath(CoreGameModID).c_str());
    as_context->ExportDocs(path);
    
    // Load script and run init function
    as_context->LoadScript(script_path);

    as_context->CallScriptFunction(as_funcs.init);
}

void ScriptableCampaign::Dispose() {
    as_context->CallScriptFunction(as_funcs.dispose);
    delete as_context;
}

void ScriptableCampaign::Update() {
    if(as_context) {
        as_context->CallScriptFunction(as_funcs.update);
    } 
}

void ScriptableCampaign::EnterCampaign() {
    if(as_context) {
        as_context->CallScriptFunction(as_funcs.enter_campaign);
    } 
}

void ScriptableCampaign::EnterLevel() {
    if(as_context) {
        as_context->CallScriptFunction(as_funcs.enter_level);
    } 
}

void ScriptableCampaign::LeaveCampaign() {
    if(as_context) {
        as_context->CallScriptFunction(as_funcs.leave_campaign);
    } 
}

void ScriptableCampaign::LeaveLevel() {
    if(as_context) {
        as_context->CallScriptFunction(as_funcs.leave_level);
    } 
}

void ScriptableCampaign::ReceiveMessage(std::string message) {
    if(as_context) {
        ASArglist args;
        args.AddObject(&message);
        as_context->CallScriptFunction(as_funcs.receive_message, &args);
    }
}

void ScriptableCampaign::WindowResized(ivec2 value ) {
    ASArglist args;
    args.Add(value[0]);
    args.Add(value[1]);
    as_context->CallScriptFunction(as_funcs.set_window_dimensions, &args);
}

std::string ScriptableCampaign::GetCampaignID() {
    return campaign_id;
}