aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2023-08-26 11:34:03 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2023-08-26 11:34:03 +0200
commitd4283a02e30348e73bce786e67e2b1a17955c679 (patch)
tree27576b3a1b71ea4c73f782f891012ea75880c364
parent03adccc9ec7a84a08f9d62287b42e09ce2b2963e (diff)
feat: Move to destination
-rw-r--r--geometry.go19
-rw-r--r--main.go24
-rw-r--r--move.go6
-rw-r--r--person.go17
4 files changed, 54 insertions, 12 deletions
diff --git a/geometry.go b/geometry.go
index 89eb1ed..0d90c6f 100644
--- a/geometry.go
+++ b/geometry.go
@@ -1,6 +1,21 @@
package main
-import "image"
+import (
+ "image"
+ "math"
+)
-type Rectangle = image.Rectangle
type Point = image.Point
+type Rectangle = image.Rectangle
+
+func Distance(p1, p2 Point) float64 {
+ return math.Sqrt(math.Pow(float64(p1.X-p2.X), 2) + math.Pow(float64(p1.Y-p2.Y), 2))
+}
+
+func Length(p Point) float64 {
+ return Distance(p, Point{0, 0})
+}
+
+func Normalize(p Point) Point {
+ return p.Div(int(Length(p)))
+}
diff --git a/main.go b/main.go
index 49e9d02..60a3b76 100644
--- a/main.go
+++ b/main.go
@@ -1,7 +1,6 @@
package main
import (
- "fmt"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
@@ -28,33 +27,38 @@ type Game struct {
}
func (g *Game) Update() error {
+ if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
+ x, y := ebiten.CursorPosition()
+ tileClick := Point{x - soil.Position.X, y - soil.Position.Y}
+ for _, p := range g.Persons {
+ if p.IsSelected {
+ p.MoveTo(tileClick)
+ }
+ }
+ }
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
tileClick := Point{x - soil.Position.X, y - soil.Position.Y}
- fmt.Println("Click: ", tileClick)
for _, p := range g.Persons {
p.IsSelected = false
- fmt.Println("Person: ", p.Position)
if tileClick.In(p.CollisionBounds()) {
- fmt.Println("Person selected")
p.IsSelected = true
}
}
}
+ for _, p := range g.Persons {
+ p.Update()
+ }
return nil
}
-var shallowWaterColor = color.RGBA{0x80, 0xa0, 0xc0, 0xff}
-
func (g *Game) Draw(screen *ebiten.Image) {
- screen.Fill(shallowWaterColor)
+ screen.Fill(color.RGBA{0x80, 0xa0, 0xc0, 0xff})
soil.Fill(soilColor)
for _, p := range g.Persons {
bounds := p.Image().Bounds()
- fmt.Println("Person bounds: ", bounds)
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(p.Position.X-bounds.Dx()/2), float64(p.Position.Y-bounds.Dy()/2))
- fmt.Println("Person position:", p.Position, "Draw geom: ", op.GeoM)
soil.DrawImage(p.Image(), op)
}
op := &ebiten.DrawImageOptions{}
@@ -67,7 +71,7 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeigh
}
func main() {
- ebiten.SetWindowSize(1280, 960)
+ ebiten.SetWindowSize(640, 480)
ebiten.SetWindowTitle("Age of Empire")
game := &Game{}
mainPerson := NewPerson(Point{soil.Image.Bounds().Dx() / 2, soil.Image.Bounds().Dy() / 2})
diff --git a/move.go b/move.go
new file mode 100644
index 0000000..916e580
--- /dev/null
+++ b/move.go
@@ -0,0 +1,6 @@
+package main
+
+type Move struct {
+ IsActive bool
+ Destination Point
+}
diff --git a/person.go b/person.go
index 0bf0992..b1248e2 100644
--- a/person.go
+++ b/person.go
@@ -10,6 +10,7 @@ type Person struct {
size Point
Position Point
IsSelected bool
+ move Move
image *ebiten.Image
selectedImage *ebiten.Image
}
@@ -38,3 +39,19 @@ func (p Person) CollisionBounds() Rectangle {
p.Position.Add(p.size.Div(2)),
}
}
+
+func (p *Person) MoveTo(destination Point) {
+ p.move = Move{IsActive: true, Destination: destination}
+}
+
+func (p *Person) Update() {
+ speed := 2
+ if p.move.IsActive {
+ remainingMove := p.move.Destination.Sub(p.Position)
+ delta := remainingMove.Mul(speed).Div(int(Length(remainingMove)))
+ p.Position = p.Position.Add(delta)
+ if Distance(p.Position, p.move.Destination) < float64(speed) {
+ p.move.IsActive = false
+ }
+ }
+}