-
Notifications
You must be signed in to change notification settings - Fork 18
/
character_render.go
227 lines (187 loc) · 6.54 KB
/
character_render.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
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
package system
import (
"math"
"strconv"
"time"
"github.com/EngoEngine/ecs"
"github.com/go-gl/mathgl/mgl32"
"github.com/rs/zerolog/log"
"github.com/project-midgard/midgarts/internal/character"
"github.com/project-midgard/midgarts/internal/character/actionindex"
"github.com/project-midgard/midgarts/internal/character/actionplaymode"
"github.com/project-midgard/midgarts/internal/character/directiontype"
"github.com/project-midgard/midgarts/internal/component"
"github.com/project-midgard/midgarts/internal/entity"
"github.com/project-midgard/midgarts/internal/fileformat/act"
"github.com/project-midgard/midgarts/internal/fileformat/grf"
"github.com/project-midgard/midgarts/internal/fileformat/spr"
"github.com/project-midgard/midgarts/internal/graphic"
"github.com/project-midgard/midgarts/internal/graphic/geometry"
"github.com/project-midgard/midgarts/internal/system/opengl"
)
const (
SpriteScaleFactor = float32(1.0)
FixedCameraDirection = 6
)
type CharacterRenderable interface {
ecs.BasicFace
component.CharacterStateComponentFace
component.CharacterAttachmentComponentFace
}
type CharacterRenderSystem struct {
grfFile *grf.File
characters map[string]*entity.Character
RenderCommands *opengl.RenderCommands
textureProvider graphic.TextureProvider
}
func NewCharacterRenderSystem(grfFile *grf.File, textureProvider graphic.TextureProvider) *CharacterRenderSystem {
return &CharacterRenderSystem{
grfFile: grfFile,
characters: map[string]*entity.Character{},
RenderCommands: &opengl.RenderCommands{
Sprites: []opengl.SpriteRenderCommand{},
},
textureProvider: textureProvider,
}
}
func (s *CharacterRenderSystem) Update(dt float32) {
s.RenderCommands.Sprites = []opengl.SpriteRenderCommand{}
for _, char := range s.characters {
s.renderCharacter(dt, char)
}
}
func (s *CharacterRenderSystem) AddByInterface(o ecs.Identifier) {
char := o.(*entity.Character)
s.Add(char)
}
func (s *CharacterRenderSystem) Add(char *entity.Character) {
cmp, err := component.NewCharacterAttachmentComponent(s.grfFile, component.CharacterAttachmentComponentConfig{
Gender: char.Gender,
JobSpriteID: char.JobSpriteID,
HeadIndex: char.HeadIndex,
EnableShield: char.HasShield,
ShieldSpriteName: char.ShieldSpriteName,
})
if err != nil {
log.Fatal().Err(err).Send()
}
char.SetCharacterAttachmentComponent(cmp)
s.characters[strconv.Itoa(int(char.ID()))] = char
}
func (s *CharacterRenderSystem) Remove(e ecs.BasicEntity) {
delete(s.characters, strconv.Itoa(int(e.ID())))
}
func (s *CharacterRenderSystem) renderCharacter(dt float32, char *entity.Character) {
offset := [2]float32{0, 0}
direction := int(char.Direction) + directiontype.DirectionTable[FixedCameraDirection]%8
behind := direction > 1 && direction < 6
renderShield := char.HasShield && char.ActionIndex == actionindex.StandBy
if char.ActionIndex != actionindex.Dead && char.ActionIndex != actionindex.Sitting {
s.renderAttachment(dt, char, character.AttachmentShadow, &offset)
}
if behind && renderShield {
s.renderAttachment(dt, char, character.AttachmentShield, &offset)
}
s.renderAttachment(dt, char, character.AttachmentBody, &offset)
s.renderAttachment(dt, char, character.AttachmentHead, &offset)
if !behind && renderShield {
s.renderAttachment(dt, char, character.AttachmentShield, &offset)
}
}
func (s *CharacterRenderSystem) renderAttachment(
dt float32,
char *entity.Character,
elem character.AttachmentType,
offset *[2]float32,
) {
var actions []*act.Action
if actions = char.Files[elem].ACT.Actions; len(actions) == 0 {
return
}
idx := (int(char.ActionIndex) + (int(char.Direction)+directiontype.DirectionTable[FixedCameraDirection])%8) % len(actions)
action := actions[idx]
frameCount := int64(len(action.Frames))
timeNeededForOneFrame := int64(float64(action.Delay) * (1.0 / char.FPSMultiplier))
if char.ForcedDuration != 0 {
timeNeededForOneFrame = int64(char.ForcedDuration) / frameCount
}
timeNeededForOneFrame = int64(math.Max(float64(timeNeededForOneFrame), 100))
elapsedTime := time.Since(char.AnimationStartedAt).Milliseconds() - int64(dt)
realIndex := elapsedTime / timeNeededForOneFrame
var frameIndex int64
switch char.PlayMode {
case actionplaymode.Repeat:
frameIndex = realIndex % frameCount
break
}
// Ignore "doridori" animation
if len(action.Frames) == 3 {
frameIndex = 0
}
var frame *act.ActionFrame
if frame = action.Frames[frameIndex]; len(frame.Layers) == 0 {
*offset = [2]float32{0, 0}
return
}
position := [2]float32{0, 0}
if len(frame.Positions) > 0 &&
elem != character.AttachmentBody &&
elem != character.AttachmentShield {
position[0] = offset[0] - float32(frame.Positions[0][0])
position[1] = offset[1] - float32(frame.Positions[0][1])
}
// Render all layers
for _, layer := range frame.Layers {
if layer.SpriteFrameIndex < 0 {
continue
}
s.renderLayer(char, layer, char.Files[elem].SPR, position)
}
// Save offset reference
if len(frame.Positions) > 0 {
*offset = [2]float32{
float32(frame.Positions[0][0]),
float32(frame.Positions[0][1]),
}
}
char.AnimationDelay = time.Duration(action.DurationMilliseconds) * time.Millisecond
}
func (s *CharacterRenderSystem) renderLayer(
char *entity.Character,
layer *act.ActionFrameLayer,
spr *spr.SpriteFile,
offset [2]float32,
) {
frameIndex := character.SpriteIndex(layer.SpriteFrameIndex)
if frameIndex < 0 {
return
}
texture, err := s.textureProvider.NewTextureFromRGBA(spr.ImageAt(frameIndex))
if err != nil {
log.Fatal().Err(err).Send()
}
frame := spr.Frames[frameIndex]
width, height := float32(frame.Width), float32(frame.Height)
width *= layer.Scale[0] * SpriteScaleFactor * geometry.OnePixelSize
height *= layer.Scale[1] * SpriteScaleFactor * geometry.OnePixelSize
rot := float64(layer.Angle) * (math.Pi / 180)
offset = [2]float32{
(float32(layer.Position[0]) + offset[0]) * geometry.OnePixelSize,
(float32(layer.Position[1]) + offset[1]) * geometry.OnePixelSize,
}
cmd := opengl.SpriteRenderCommand{
Scale: layer.Scale,
Size: mgl32.Vec2{width, height},
Position: char.Position(),
Offset: mgl32.Vec2{offset[0], offset[1]},
RotationRadians: float32(rot),
Texture: texture,
FlipVertically: layer.Mirrored,
}
// This is the current API to render a shaders. Commands will
// be collected by the lower-level rendering system (OpenGL).
s.renderSpriteCommand(cmd)
}
func (s *CharacterRenderSystem) renderSpriteCommand(cmd ...opengl.SpriteRenderCommand) {
s.RenderCommands.Sprites = append(s.RenderCommands.Sprites, cmd...)
}