-
Notifications
You must be signed in to change notification settings - Fork 0
/
flame.go
148 lines (134 loc) · 3.95 KB
/
flame.go
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
/*
* flame.go
*
* Copyright 2018-2023 Dariusz Sikora <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
// flame package provides structs for module and chapater.
package flame
import (
"github.com/isangeles/flame/character"
"github.com/isangeles/flame/data/res"
"github.com/isangeles/flame/serial"
)
const (
Name, Version = "Flame", "0.1.0-dev"
)
// Module struct represents game module.
type Module struct {
res *res.ResourcesData
conf *ModuleConfig
chapter *Chapter
changeChapterEvents []func(ob *character.Character)
}
// NewModule creates new game module from specified data.
func NewModule(data res.ModuleData) *Module {
m := new(Module)
m.conf = new(ModuleConfig)
m.Apply(data)
return m
}
// Update updates module.
func (m *Module) Update(delta int64) {
if m.Chapter() == nil {
return
}
m.Chapter().Update(delta)
for _, c := range m.Chapter().Characters() {
if len(c.ChapterID()) > 0 && c.ChapterID() != m.Chapter().ID() {
for _, ev := range m.changeChapterEvents {
ev(c)
}
}
}
}
// SetChapter sets specified chapter as current chapter.
func (m *Module) SetChapter(chapter *Chapter) {
m.chapter = chapter
}
// Chapter returns current module chapter.
func (m *Module) Chapter() *Chapter {
return m.chapter
}
// Conf returns module configuration.
func (m *Module) Conf() *ModuleConfig {
return m.conf
}
// Object returns game object with specified ID and serial
// or nil if no such object was found.
func (m *Module) Object(id, serial string) serial.Serialer {
char := m.Chapter().Character(id, serial)
if char != nil {
return char
}
ob := m.Chapter().AreaObject(id, serial)
if ob != nil {
return ob
}
return nil
}
// Resources returns module resources.
func (m *Module) Resources() *res.ResourcesData {
return m.res
}
// AddChangeChapterEvent adds function to trigger when chapter
// change is required.
func (m *Module) AddChangeChapterEvent(event func(char *character.Character)) {
m.changeChapterEvents = append(m.changeChapterEvents, event)
}
// Apply applies specified data on the module.
// Also, adds module resources to resources
// base in res package.
func (m *Module) Apply(data res.ModuleData) {
if len(data.Config["id"]) > 0 {
m.conf.ID = data.Config["id"][0]
}
if len(data.Config["path"]) > 0 {
m.conf.Path = data.Config["path"][0]
}
if len(data.Config["chapter"]) > 0 {
m.conf.Chapter = data.Config["chapter"][0]
}
m.res = &data.Resources
res.Add(*m.res)
if m.Chapter() == nil || m.Chapter().Conf().ID != data.Chapter.ID {
chapter := NewChapter(m, data.Chapter)
m.SetChapter(chapter)
return
}
m.Chapter().Apply(data.Chapter)
}
// Data creates data resource for module.
func (m *Module) Data() res.ModuleData {
data := res.ModuleData{ID: m.Conf().ID}
data.Config = make(map[string][]string)
data.Config["id"] = []string{m.Conf().ID}
data.Config["path"] = []string{m.Conf().Path}
data.Config["chapter"] = []string{m.Chapter().Conf().ID}
data.Chapter = m.Chapter().Data()
data.Resources = *m.res
// Remove old characters from resources, besides basic ones.
data.Resources.Characters = make([]res.CharacterData, 0)
for _, c := range m.Resources().Characters {
if len(c.Serial) < 1 {
data.Resources.Characters = append(data.Resources.Characters, c)
}
}
return data
}