aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2023-09-14 08:07:34 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2023-09-14 08:07:34 +0200
commit21287052a3809c2247be4243f91a32e2665961ed (patch)
tree364dc9a1c2a41ed9cd28d7b8d1978391ab5acbbc /main.go
parentea2dbf13c21185da5693ebf4d37369a6fc2f73ff (diff)
feat: Change cursor on hover
Diffstat (limited to 'main.go')
-rw-r--r--main.go61
1 files changed, 48 insertions, 13 deletions
diff --git a/main.go b/main.go
index 68df4f2..10702a0 100644
--- a/main.go
+++ b/main.go
@@ -26,11 +26,26 @@ func NewTile(size Point, position Point, color color.Color) Tile {
image.Fill(color)
return Tile{Size: size, Position: position, Image: image}
}
-
func (t Tile) CollisionBounds() Rectangle {
return Rectangle{t.Position, t.Position.Add(t.Size)}
}
+type Block struct {
+ Tile
+ typ BlockType
+}
+
+type BlockType int
+
+const (
+ WallBlock BlockType = iota
+ IronBlock
+)
+
+func (b Block) CollisionBounds() Rectangle {
+ return b.Tile.CollisionBounds().Sub(b.Size.Div(2))
+}
+
var soilColor = color.RGBA{0x60, 0x40, 0x20, 0xff}
var soil = NewTile(Point{2800, 2000}, Point{200, 200}, soilColor)
var wallButton = NewTile(Point{100, 100}, Point{50, 50}, color.RGBA{0x00, 0x00, 0x00, 0xff})
@@ -44,7 +59,15 @@ type Game struct {
Persons []*Person
CurrentAction Action
Selection Selection
- Walls []Tile
+ Blocks []Block
+}
+
+func (g *Game) getBlocked() map[Point]bool {
+ blocked := map[Point]bool{}
+ for _, w := range g.Blocks {
+ blocked[w.Position] = true
+ }
+ return blocked
}
func (g *Game) updateSelecting(cursor Point) {
@@ -54,10 +77,6 @@ func (g *Game) updateSelecting(cursor Point) {
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
destination := cursor.Sub(soil.Position).Div(100).Mul(100).Add(Point{50, 50})
- blocked := map[Point]bool{}
- for _, w := range g.Walls {
- blocked[w.Position] = true
- }
for _, p := range g.Persons {
if p.IsSelected {
futureBounds := Rectangle{
@@ -65,7 +84,7 @@ func (g *Game) updateSelecting(cursor Point) {
destination.Add(p.size.Div(2)),
}
if futureBounds.In(soil.Bounds()) {
- p.MoveTo(destination, blocked)
+ p.MoveTo(destination, g.getBlocked())
}
}
}
@@ -97,7 +116,7 @@ func (g *Game) updatePlacingWall(cursor Point) {
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
if cursor.In(soil.Bounds()) {
position := cursor.Sub(soil.Position).Div(100).Mul(100).Add(Point{50, 50})
- g.Walls = append(g.Walls, NewTile(Point{100, 100}, position, color.RGBA{0x00, 0x00, 0x00, 0xff}))
+ g.Blocks = append(g.Blocks, Block{NewTile(Point{100, 100}, position, color.RGBA{0x00, 0x00, 0x00, 0xff}), WallBlock})
}
}
}
@@ -127,17 +146,34 @@ func (g *Game) Update() error {
for _, p := range g.Persons {
p.Update()
}
+ soilCursor := cursor.Sub(soil.Position)
+ isIronHovered := false
+ for _, w := range g.Blocks {
+ if w.typ == IronBlock {
+ if soilCursor.In(w.CollisionBounds()) {
+ isIronHovered = true
+ }
+ }
+ }
+ if isIronHovered {
+ ebiten.SetCursorShape(ebiten.CursorShapeCrosshair)
+ } else {
+ ebiten.SetCursorShape(ebiten.CursorShapeDefault)
+ }
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
+ x, y := ebiten.CursorPosition()
+ screenClick := Point{x, y}
screen.Fill(color.RGBA{0x80, 0xa0, 0xc0, 0xff})
soil.Fill(soilColor)
- for _, w := range g.Walls {
+ for _, w := range g.Blocks {
bounds := w.Image.Bounds()
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(w.Position.X-bounds.Dx()/2), float64(w.Position.Y-bounds.Dy()/2))
soil.DrawImage(w.Image, op)
+
}
for _, p := range g.Persons {
bounds := p.Image().Bounds()
@@ -159,8 +195,6 @@ func (g *Game) Draw(screen *ebiten.Image) {
op.GeoM.Translate(float64(wallButton.Position.X), float64(wallButton.Position.Y))
screen.DrawImage(wallButton.Image, op)
if g.Selection.IsActive {
- x, y := ebiten.CursorPosition()
- screenClick := Point{x, y}
vector.StrokeRect(screen, float32(g.Selection.Start.X), float32(g.Selection.Start.Y), float32(screenClick.X-g.Selection.Start.X), float32(screenClick.Y-g.Selection.Start.Y), 10.0, color.RGBA{256 * 3 / 16, 256 * 3 / 16, 256 * 3 / 16, 256 / 4}, true)
}
}
@@ -174,13 +208,14 @@ func main() {
ebiten.SetWindowTitle("Age of Empire")
game := &Game{}
mainPerson := NewPerson(Point{soil.Image.Bounds().Dx() / 2, soil.Image.Bounds().Dy() / 2})
- nonPlayerPerson := NewPerson(Point{400, 400})
+ nonPlayerPerson := NewPerson(Point{450, 450})
game.Persons = append(game.Persons, &mainPerson)
game.Persons = append(game.Persons, &nonPlayerPerson)
game.CurrentAction = Selecting
for i := 0; i < 10; i++ {
- game.Walls = append(game.Walls, NewTile(Point{100, 100}, Point{350 + i*100, 350}, color.RGBA{0x00, 0x00, 0x00, 0xff}))
+ game.Blocks = append(game.Blocks, Block{NewTile(Point{100, 100}, Point{350 + i*100, 350}, color.RGBA{0x00, 0x00, 0x00, 0xff}), WallBlock})
}
+ game.Blocks = append(game.Blocks, Block{NewTile(Point{100, 100}, Point{1250, 1250}, color.RGBA{0x80, 0x80, 0x80, 0xff}), IronBlock})
if err := ebiten.RunGame(game); err != nil {
panic(err)
}