@@ -47,17 +47,23 @@ var (
47
47
offset = [2 ]int {0 , 0 }
48
48
gridSize = [2 ]int {8 , 8 }
49
49
tileSize = [2 ]int {64 , 64 }
50
- box = [4 ]float64 {70 , 70 , 24 , 32 }
50
+ playerBox = [4 ]float64 {70 , 70 , 24 , 32 } // x, y, w, h
51
51
vel = [2 ]float64 {0 , 4 }
52
- cam = kamera .NewCamera (box [0 ], box [1 ], float64 (screenWidth ), float64 (screenHeight ))
52
+ cam = kamera .NewCamera (playerBox [0 ], playerBox [1 ], float64 (screenWidth ), float64 (screenHeight ))
53
53
controller = NewPlayerController ()
54
54
collider = tilecollider .NewCollider (tileMap , tileSize [0 ], tileSize [1 ])
55
+ tileDIO = & ebiten.DrawImageOptions {}
56
+ playerDIO = & ebiten.DrawImageOptions {}
57
+ tileImage = ebiten .NewImage (tileSize [0 ], tileSize [1 ])
58
+ playerImage = ebiten .NewImage (24 , 32 )
55
59
)
56
60
57
61
func init () {
58
62
cam .SmoothType = kamera .SmoothDamp
59
63
cam .ShakeEnabled = true
60
64
65
+ tileImage .Fill (color.RGBA {0 , 0 , 255 , 0 })
66
+ playerImage .Fill (color.Gray {100 })
61
67
controller .SetPhyicsScale (2.2 )
62
68
}
63
69
@@ -130,13 +136,13 @@ func (g *Game) Update() error {
130
136
if inpututil .IsKeyJustPressed (ebiten .KeyTab ) {
131
137
switch cam .SmoothType {
132
138
case kamera .None :
133
- cam .SetCenter (box [0 ]+ box [2 ]/ 2 , box [1 ]+ box [3 ]/ 2 )
139
+ cam .SetCenter (playerBox [0 ]+ playerBox [2 ]/ 2 , playerBox [1 ]+ playerBox [3 ]/ 2 )
134
140
cam .SmoothType = kamera .Lerp
135
141
case kamera .Lerp :
136
- cam .SetCenter (box [0 ]+ box [2 ]/ 2 , box [1 ]+ box [3 ]/ 2 )
142
+ cam .SetCenter (playerBox [0 ]+ playerBox [2 ]/ 2 , playerBox [1 ]+ playerBox [3 ]/ 2 )
137
143
cam .SmoothType = kamera .SmoothDamp
138
144
case kamera .SmoothDamp :
139
- cam .SetCenter (box [0 ]+ box [2 ]/ 2 , box [1 ]+ box [3 ]/ 2 )
145
+ cam .SetCenter (playerBox [0 ]+ playerBox [2 ]/ 2 , playerBox [1 ]+ playerBox [3 ]/ 2 )
140
146
cam .SmoothType = kamera .None
141
147
}
142
148
}
@@ -146,10 +152,10 @@ func (g *Game) Update() error {
146
152
}
147
153
vel = controller .ProcessVelocity (vel )
148
154
dx , dy := collider .Collide (
149
- box [0 ],
150
- box [1 ],
151
- box [2 ],
152
- box [3 ],
155
+ playerBox [0 ],
156
+ playerBox [1 ],
157
+ playerBox [2 ],
158
+ playerBox [3 ],
153
159
vel [0 ],
154
160
vel [1 ],
155
161
func (infos []tilecollider.CollisionInfo [uint8 ], _ , _ float64 ) {
@@ -168,10 +174,10 @@ func (g *Game) Update() error {
168
174
},
169
175
)
170
176
171
- Translate (& box , dx , dy )
177
+ Translate (& playerBox , dx , dy )
172
178
173
179
// Update camera
174
- cam .LookAt (box [0 ]+ box [2 ]/ 2 , box [1 ]+ box [3 ]/ 2 )
180
+ cam .LookAt (playerBox [0 ]+ playerBox [2 ]/ 2 , playerBox [1 ]+ playerBox [3 ]/ 2 )
175
181
176
182
return nil
177
183
}
@@ -188,27 +194,17 @@ func (g *Game) Draw(s *ebiten.Image) {
188
194
for y , row := range tileMap {
189
195
for x , value := range row {
190
196
if value != 0 {
191
- px , py := float64 (x * tileSize [0 ]), float64 (y * tileSize [1 ])
192
- geom := & ebiten.GeoM {}
193
- cam .ApplyCameraTransform (geom )
194
- px , py = geom .Apply (px , py )
195
- vector .StrokeRect (
196
- s ,
197
- float32 (px ),
198
- float32 (py ),
199
- float32 (tileSize [0 ]),
200
- float32 (tileSize [1 ]),
201
- 1 ,
202
- color.RGBA {R : 80 , G : 80 , B : 200 , A : 255 },
203
- false ,
204
- )
197
+ tileDIO .GeoM .Reset ()
198
+ tileDIO .GeoM .Translate (float64 (x * tileSize [0 ]), float64 (y * tileSize [1 ]))
199
+ cam .Draw (tileImage , tileDIO , s )
205
200
}
206
201
}
207
202
}
208
203
209
204
// Draw player
210
- x , y := cam .ApplyCameraTransformToPoint (box [0 ], box [1 ])
211
- vector .DrawFilledRect (s , float32 (x ), float32 (y ), float32 (box [2 ]), float32 (box [3 ]), color.Gray {100 }, false )
205
+ playerDIO .GeoM .Reset ()
206
+ playerDIO .GeoM .Translate (playerBox [0 ], playerBox [1 ])
207
+ cam .Draw (playerImage , playerDIO , s )
212
208
213
209
// Draw camera crosshair
214
210
cx , cy := float32 (screenWidth / 2 ), float32 (screenHeight / 2 )
0 commit comments