/
square_entity.go
110 lines (96 loc) · 2 KB
/
square_entity.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
package main
import (
"github.com/veandco/go-sdl2/sdl"
"github.com/xamino/orbengine"
)
/*
square is a simple test entity.
*/
type square struct {
id string
rect *sdl.Rect
color *sdl.Color
pos *sdl.Point
rot float64
}
func newSquare(id string, posX, posY, width, height int32, r, g, b uint8, rotation float64) *square {
pos := &sdl.Point{X: posX, Y: posY}
rect := &sdl.Rect{
X: posX - (width / 2),
Y: posY - (height / 2),
W: width,
H: height}
color := &sdl.Color{R: r, G: g, B: b, A: 255}
return &square{
id: id,
rect: rect,
color: color,
pos: pos,
rot: rotation}
}
func (s *square) Identification() string {
return s.id
}
func (s *square) Render(renderer *orbengine.Renderer) {
renderer.SetDrawColor(s.color.R, s.color.G, s.color.B, s.color.A)
renderer.FillRect(nil) // fill nil --> fill everything
}
func (s *square) Redraw() bool {
// green needs to be redrawn frequently because it is color animated
if s.color.G == 255 {
return true
}
return false
}
func (s *square) Position() *sdl.Point {
return s.pos
}
func (s *square) Offset() *sdl.Point {
return nil
}
func (s *square) Scale() int {
return 1
}
func (s *square) Width() int {
return int(s.rect.W)
}
func (s *square) Height() int {
return int(s.rect.H)
}
func (s *square) Rotation() float64 {
return s.rot
}
func (s *square) Layer() int {
return -1
}
func (s *square) Action() {
// have red move (green check to avoid green moving every now and then)
if s.color.R == 255 && s.color.G == 0 {
// reset if reached border
if s.pos.X < int32(-s.Width()) {
// log.Println("Red: reset")
s.pos.X = 600
s.rect.X = 600
}
// TODO couple these two values somehow
s.pos.X -= 5
s.rect.X -= 5
}
// have blue continously turn
if s.color.B == 255 {
if s.rot >= 360 {
// log.Println("Blue: reset")
s.rot = 0
}
s.rot = (s.rot + 5)
}
// have green animate color
if s.color.G == 255 {
if s.color.R >= 255 {
// log.Println("Green: reset")
s.color.R = 0
} else {
s.color.R += 5
}
}
}