/
level.go
157 lines (135 loc) · 3.42 KB
/
level.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
package level
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/xackery/aseprite"
"github.com/xackery/magnets/font"
"github.com/xackery/magnets/global"
"github.com/xackery/magnets/library"
"github.com/xackery/magnets/weapon"
)
type Level struct {
Data *weapon.WeaponData
layer *aseprite.Layer
cell *aseprite.Cell
x float64
y float64
width float64
height float64
isVisible bool
isDead bool
percent float64
}
func New(w *weapon.WeaponData) (*Level, error) {
if w == nil {
return nil, fmt.Errorf("weapon instance not found: %v", w)
}
if w.Icon == nil {
return nil, fmt.Errorf("icon not found on weapon %v", w)
}
layer, err := library.Layer(w.Icon.SpriteName, w.Icon.LayerName)
if err != nil {
return nil, fmt.Errorf("library.Layer: %w", err)
}
if len(layer.Cells) < 1 {
return nil, fmt.Errorf("no cells found on layer %s", w.Icon.LayerName)
}
b := &Level{
layer: layer,
width: float64(layer.SpriteWidth),
height: float64(layer.SpriteHeight),
Data: w,
//percent: 0,
isVisible: true,
}
cell := layer.Cells[0]
b.cell = cell
//b.swidth = b.width * int(global.ScreenScaleX())
//b.sheight = b.height * int(global.ScreenScaleY())
//b.xOffset -= b.width / 2
levels = append(levels, b)
return b, nil
}
func (n *Level) Draw(screen *ebiten.Image, yOffset float64) error {
if !n.isVisible {
return nil
}
n.x = float64(n.cell.PositionX) + float64(global.ScreenWidth())/2 - 100
n.y = float64(n.cell.PositionY) + 100 + float64(yOffset)
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(2, 2)
op.GeoM.Translate(n.x, n.y)
screen.DrawImage(n.cell.EbitenImage, op)
op.GeoM.Scale(2, 2)
op.GeoM.Translate(n.x, n.y)
screen.DrawImage(n.cell.EbitenImage, op)
// op.ColorM.Reset()
// op.GeoM.Reset()
//op.GeoM.Scale(n.width*n.percent, n.height)
//op.GeoM.Translate(n.x, n.y)
//op.GeoM.Translate(float64(n.cell.PositionX), float64(n.cell.PositionY))
// screen.DrawImage(n.cell.EbitenImage, op)
//bounds := text.BoundString(font.NormalFont(), n.data.Name())
//x, y := int(n.width/2)-bounds.Min.X-bounds.Dx()/2, int(n.height/2)-bounds.Min.Y-bounds.Dy()/2
x := n.x + 50
y := n.y + 10
text.Draw(screen, n.Data.Name(), font.NormalFont(), int(x-1), int(y-1), color.Black)
text.Draw(screen, n.Data.Name(), font.NormalFont(), int(x), int(y), color.White)
y += 20
text.Draw(screen, n.Data.Description, font.SmallFont(), int(x-1), int(y-1), color.Black)
text.Draw(screen, n.Data.Description, font.SmallFont(), int(x), int(y), color.White)
return nil
}
func (n *Level) SetDead(state bool) {
n.isDead = state
}
func (n *Level) SetPosition(x, y float64) {
n.x = x
n.y = y
}
func (n *Level) SetPercent(value float64) {
if value > 1 {
value = 1
}
if value < 0 {
value = 0
}
n.percent = value
}
func (n *Level) SetWidth(value float64) {
if value == 0 {
n.width = float64(n.layer.SpriteWidth)
return
}
n.width = value
}
func (n *Level) SetHeight(value float64) {
if value == 0 {
n.height = float64(n.layer.SpriteHeight)
return
}
n.height = value
}
func (n *Level) SetVisible(value bool) {
n.isVisible = value
}
func (n *Level) IsHit(x, y float64) bool {
if !n.isVisible {
return false
}
if n.x > x {
return false
}
if n.x+float64(n.layer.SpriteWidth)+200 < x {
return false
}
if n.y > y {
return false
}
if n.y+(float64(n.layer.SpriteHeight)*2) < y {
return false
}
return true
}