Skip to content

Commit cf0ae38

Browse files
committed
Allow Sprite and Fonts to be tinted
Long awaited feature!
1 parent 0717c01 commit cf0ae38

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

game/embark/manager.go

-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ func (em *Manager) addRoadwayEntity(key geom.Key, dir geom.DirectionType) {
183183
X: sx, Y: sy,
184184
W: 20, H: 12,
185185
})
186-
187186
}
188187

189188
func (em *Manager) addFeatureEntity(feat Feature, m, n, layer int) {

game/font.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ type Font struct {
1616
// Align string // Align should be (left|wrap|center|right)
1717
// Width int // Width is relevant to wrap, center, and right Alignments
1818

19-
// N.B. Don't add Color here! A Color tint would need to be applied to a
20-
// sprite anyway in order to tint the underlying Sprite-base Entities, so it
21-
// should be it's one Component that could also be applied to Sprite directly.
19+
// N.B. The color of text can be set with a Tint component. Don't be tempted
20+
// to add coloring here.
2221
}
2322

2423
// Type of this Component.
@@ -370,9 +369,9 @@ func switchRuneNormal(r rune) (w, h, x, y int) {
370369
func (s *FontSystem) construct(parent ecs.Entity) {
371370
font := s.mgr.Component(parent, "Font").(*Font)
372371
position := s.mgr.Component(parent, "Position").(*Position)
373-
scale, ok := s.mgr.Component(parent, "Scale").(*Scale)
372+
tint, _ := s.mgr.Component(parent, "Tint").(*Tint)
374373
offset, _ := s.mgr.Component(parent, "RenderOffset").(*RenderOffset)
375-
374+
scale, ok := s.mgr.Component(parent, "Scale").(*Scale)
376375
if !ok {
377376
scale = &Scale{
378377
X: 1,
@@ -418,6 +417,11 @@ func (s *FontSystem) construct(parent ecs.Entity) {
418417
Y: scale.Y,
419418
})
420419
}
420+
if tint != nil {
421+
s.mgr.AddComponent(e, &Tint{
422+
R: tint.R, G: tint.G, B: tint.B,
423+
})
424+
}
421425
s.mgr.AddComponent(e, &Position{
422426
Center: Center{
423427
X: position.Center.X + px + float64(w)/2,

game/sprite.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ type Sprite struct {
1717
OffsetX int `json:"offsetX"`
1818
OffsetY int `json:"offsetY"`
1919

20-
// This could include a color, but does not for now, as there are no uses for it.
21-
// Color *color.RGBA
22-
// NB It might make more sense to keep Color/Tint as a separate Component
23-
// so that it can be applied to non-sprite renderable things, like solid-
24-
// color or bordered shapes.
20+
// FYI, you can alter a Sprite's color with the Tint component.
2521
}
2622

2723
// Type of this Component.

game/tint.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package game
2+
3+
// Tint sets the color of the entity when it is rendered.
4+
type Tint struct {
5+
R, G, B uint8
6+
}
7+
8+
var TintPseudoBlack = Tint{R: 0x14, G: 0x1b, B: 0x27}
9+
var TintPseudoWhite = Tint{R: 0xda, G: 0xe1, B: 0xe5}
10+
11+
// Type of this Component.
12+
func (*Tint) Type() string {
13+
return "Tint"
14+
}

output/uiVisualizer.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"image"
66

7+
"github.com/griffithsh/squads/game"
78
"github.com/griffithsh/squads/ui"
89
"github.com/hajimehoshi/ebiten/v2"
910
)
@@ -229,6 +230,7 @@ func (uv *uiVisualizer) drawText(screen *ebiten.Image, value string, size ui.Tex
229230
img := img.SubImage(image.Rect(char.X, char.Y, char.X+char.Width, char.Y+char.Height)).(*ebiten.Image)
230231
op := ebiten.DrawImageOptions{}
231232
op.GeoM.Translate(x, y)
233+
op.ColorScale.Scale(float32(game.TintPseudoWhite.R)/255, float32(game.TintPseudoWhite.G)/255, float32(game.TintPseudoWhite.B)/255, 1)
232234
screen.DrawImage(img, &op)
233235
x += float64(char.Width)
234236

output/visualizer.go

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type entity struct {
6363
p *game.Position
6464
offset *game.RenderOffset
6565
scale *game.Scale
66+
tint *game.Tint
6667
repeat *game.SpriteRepeat
6768
alpha *game.Alpha
6869
}
@@ -151,6 +152,9 @@ func (r *Visualizer) getEntities(mgr *ecs.World) []entity {
151152
if scale, ok := mgr.Component(e, "Scale").(*game.Scale); ok {
152153
ent.scale = scale
153154
}
155+
if tint, ok := mgr.Component(e, "Tint").(*game.Tint); ok {
156+
ent.tint = tint
157+
}
154158
if repeat, ok := mgr.Component(e, "SpriteRepeat").(*game.SpriteRepeat); ok {
155159
ent.repeat = repeat
156160
}
@@ -329,13 +333,20 @@ func (r *Visualizer) renderEntity(e entity, focusX, focusY, zoom, screenW, scree
329333
offX, offY := float64(x), float64(y)
330334

331335
op := e.drawImageOptions(focusX, focusY, screenW/zoom, screenH/zoom, offX, offY)
336+
if e.tint != nil {
337+
op.ColorScale.Scale(float32(e.tint.R)/255, float32(e.tint.G)/255, float32(e.tint.B)/255, 1)
338+
}
332339
target.DrawImage(tile, op)
340+
333341
}
334342
}
335343
return nil
336344
}
337345

338346
op := e.drawImageOptions(focusX, focusY, screenW/zoom, screenH/zoom, 0, 0)
347+
if e.tint != nil {
348+
op.ColorScale.Scale(float32(e.tint.R)/255, float32(e.tint.G)/255, float32(e.tint.B)/255, 1)
349+
}
339350
target.DrawImage(img, op)
340351

341352
return nil

0 commit comments

Comments
 (0)