prepared copy mode

This commit is contained in:
2024-05-24 20:27:08 +02:00
parent 757b48232f
commit 9b117d3a90

66
game.go
View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"image"
"image/color" "image/color"
"log" "log"
"math/rand" "math/rand"
@@ -41,6 +42,10 @@ type Game struct {
Dragging bool // middle mouse is pressed, move canvas Dragging bool // middle mouse is pressed, move canvas
LastCursorPos []int // used to check if the user is dragging LastCursorPos []int // used to check if the user is dragging
Statefile string // load game state from it if non-nil Statefile string // load game state from it if non-nil
Markmode bool // enabled with 'c'
MarkTaken bool // true when mouse1 pressed
MarkDone bool // true when mouse1 released, copy cells between Mark+Point
Mark, Point image.Point
} }
func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) { func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
@@ -128,6 +133,15 @@ func (game *Game) CheckInput() {
os.Exit(0) os.Exit(0)
} }
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
fmt.Println("mark mode on")
game.Markmode = true
}
if game.Markmode {
return
}
if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyEnter) { if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
game.Paused = !game.Paused game.Paused = !game.Paused
} }
@@ -169,18 +183,13 @@ func (game *Game) CheckInput() {
} }
} }
func (game *Game) SaveState() {
filename := GetFilename(game.Generations)
err := game.Grids[game.Index].SaveState(filename)
if err != nil {
log.Printf("failed to save game state to %s: %s", filename, err)
}
log.Printf("saved game state to %s at generation %d\n", filename, game.Generations)
}
// Check dragging input. move the canvas with the mouse while pressing // Check dragging input. move the canvas with the mouse while pressing
// the middle mouse button, zoom in and out using the wheel. // the middle mouse button, zoom in and out using the wheel.
func (game *Game) CheckDraggingInput() { func (game *Game) CheckDraggingInput() {
if game.Markmode {
return
}
// move canvas // move canvas
if game.Dragging && !ebiten.IsMouseButtonPressed(ebiten.MouseButton1) { if game.Dragging && !ebiten.IsMouseButtonPressed(ebiten.MouseButton1) {
// release // release
@@ -248,9 +257,48 @@ func (game *Game) CheckDraggingInput() {
} }
func (game *Game) GetWorldCursorPos() image.Point {
worldX, worldY := game.Camera.ScreenToWorld(ebiten.CursorPosition())
return image.Point{
X: int(worldX) / game.Cellsize,
Y: int(worldY) / game.Cellsize,
}
}
func (game *Game) CheckMarkInput() {
if !game.Markmode {
return
}
if ebiten.IsMouseButtonPressed(ebiten.MouseButton0) {
if !game.MarkTaken {
game.Mark = game.GetWorldCursorPos()
game.MarkTaken = true
game.MarkDone = false
}
game.Point = game.GetWorldCursorPos()
fmt.Printf("Mark: %v, Current: %v\n", game.Mark, game.Point)
} else if inpututil.IsMouseButtonJustReleased(ebiten.MouseButton0) {
game.Markmode = false
game.MarkTaken = false
game.MarkDone = true
}
}
func (game *Game) SaveState() {
filename := GetFilename(game.Generations)
err := game.Grids[game.Index].SaveState(filename)
if err != nil {
log.Printf("failed to save game state to %s: %s", filename, err)
}
log.Printf("saved game state to %s at generation %d\n", filename, game.Generations)
}
func (game *Game) Update() error { func (game *Game) Update() error {
game.CheckInput() game.CheckInput()
game.CheckDraggingInput() game.CheckDraggingInput()
game.CheckMarkInput()
if !game.Paused || game.RunOneStep { if !game.Paused || game.RunOneStep {
game.UpdateCells() game.UpdateCells()