122 lines
2.0 KiB
Go
122 lines
2.0 KiB
Go
package world
|
|
|
|
import (
|
|
"image/color"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
|
)
|
|
|
|
type SurfaceTag int
|
|
|
|
const (
|
|
TagGround SurfaceTag = iota
|
|
TagWall
|
|
TagPlatform
|
|
TagHazard
|
|
)
|
|
|
|
type Surface struct {
|
|
X float64
|
|
Y float64
|
|
Width float64
|
|
Height float64
|
|
Tag SurfaceTag
|
|
Color color.NRGBA
|
|
}
|
|
|
|
func (s *Surface) Bounds() (x, y, width, height float64) {
|
|
return s.X, s.Y, s.Width, s.Height
|
|
}
|
|
|
|
func (s *Surface) Contains(x, y float64) bool {
|
|
return x >= s.X && x <= s.X+s.Width && y >= s.Y && y <= s.Y+s.Height
|
|
}
|
|
|
|
func (s *Surface) IsSolid() bool {
|
|
switch s.Tag {
|
|
case TagGround, TagWall, TagPlatform:
|
|
return true
|
|
case TagHazard:
|
|
return false
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (s *Surface) IsWalkable() bool {
|
|
switch s.Tag {
|
|
case TagGround, TagPlatform:
|
|
return true
|
|
case TagWall, TagHazard:
|
|
return false
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (s *Surface) Draw(screen *ebiten.Image) {
|
|
vector.DrawFilledRect(
|
|
screen,
|
|
float32(s.X),
|
|
float32(s.Y),
|
|
float32(s.Width),
|
|
float32(s.Height),
|
|
s.Color,
|
|
false,
|
|
)
|
|
}
|
|
|
|
type World struct {
|
|
Surfaces []*Surface
|
|
}
|
|
|
|
func NewWorld() *World {
|
|
return &World{
|
|
Surfaces: make([]*Surface, 0),
|
|
}
|
|
}
|
|
|
|
func (w *World) AddSurface(surface *Surface) {
|
|
w.Surfaces = append(w.Surfaces, surface)
|
|
}
|
|
|
|
func (w *World) GetSurfacesWithTag(tag SurfaceTag) []*Surface {
|
|
var result []*Surface
|
|
for _, s := range w.Surfaces {
|
|
if s.Tag == tag {
|
|
result = append(result, s)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (w *World) GetSurfacesAt(x, y float64) []*Surface {
|
|
var result []*Surface
|
|
for _, s := range w.Surfaces {
|
|
if s.Contains(x, y) {
|
|
result = append(result, s)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (w *World) CheckCollision(x, y, width, height float64) *Surface {
|
|
for _, s := range w.Surfaces {
|
|
if !s.IsSolid() {
|
|
continue
|
|
}
|
|
if x+width > s.X && x < s.X+s.Width &&
|
|
y+height > s.Y && y < s.Y+s.Height {
|
|
return s
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (w *World) Draw(screen *ebiten.Image) {
|
|
for _, s := range w.Surfaces {
|
|
s.Draw(screen)
|
|
}
|
|
}
|