Incorporating flask element.
This commit is contained in:
99
game/game.go
99
game/game.go
@@ -4,6 +4,7 @@ import (
|
||||
"fluids/elements"
|
||||
"fluids/gamedata"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"math"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
@@ -16,6 +17,7 @@ const (
|
||||
GameHeight = 360
|
||||
GameFSDW = 200
|
||||
GameFSDH = 100
|
||||
GameSims = 3 //number of total sims
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
@@ -28,6 +30,7 @@ type Game struct {
|
||||
fluidsimd *elements.FluidSimD
|
||||
fluidsim10 []*elements.FluidSim10
|
||||
alertbox *elements.Alert
|
||||
flask *elements.RoundedBottomFlask
|
||||
|
||||
//cache elements
|
||||
fluidsim10width float64
|
||||
@@ -37,6 +40,11 @@ type Game struct {
|
||||
fluidsim10angle float64 //purely for debugging
|
||||
mdown bool
|
||||
mdx, mdy int
|
||||
mdangle float64 //angle at mousedown
|
||||
mfangle float64 //last flask angle
|
||||
|
||||
//colors
|
||||
flaskcolor color.RGBA
|
||||
}
|
||||
|
||||
func NewGame() *Game {
|
||||
@@ -45,6 +53,7 @@ func NewGame() *Game {
|
||||
fluidsimidx: 0,
|
||||
alertbox: elements.NewAlert(),
|
||||
fluidsimd: elements.NewFluidSimD(),
|
||||
flask: elements.NewRoundedBottomFlask(),
|
||||
//fluidsim10: elements.NewFluidSim10(gamedata.Vector{X: GameFSDW, Y: GameFSDH}),
|
||||
//fluidsimgpt: elements.NewFlipFluidEntity(640, 480, 2, 1, 100),
|
||||
|
||||
@@ -66,6 +75,8 @@ func (g *Game) Update() error {
|
||||
case 1:
|
||||
//g.fluidsimgpt.Update()
|
||||
g.UpdateFluidsim10()
|
||||
case 2:
|
||||
g.UpdateFlask()
|
||||
default:
|
||||
break
|
||||
}
|
||||
@@ -90,6 +101,8 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
||||
g.RenderFluidSimD(screen)
|
||||
case 1:
|
||||
g.RenderFluidSim10(screen)
|
||||
case 2:
|
||||
g.RenderFlask(screen)
|
||||
default:
|
||||
break
|
||||
}
|
||||
@@ -144,8 +157,9 @@ func (g *Game) ParseInputs() {
|
||||
case 0:
|
||||
g.ManageFluidSimDInputs()
|
||||
case 1:
|
||||
//g.ManageFluidSimGPTInputs()
|
||||
g.ManageFluidSim10Inputs()
|
||||
case 2:
|
||||
g.ManageFlaskInputs()
|
||||
default:
|
||||
break
|
||||
}
|
||||
@@ -159,13 +173,13 @@ func (g *Game) ParseInputs() {
|
||||
|
||||
//swap fluid simulations
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyPageUp) {
|
||||
g.fluidsimidx = (g.fluidsimidx + 1) % 2
|
||||
g.fluidsimidx = (g.fluidsimidx + 1) % GameSims
|
||||
}
|
||||
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyPageDown) {
|
||||
g.fluidsimidx = g.fluidsimidx - 1
|
||||
if g.fluidsimidx < 0 {
|
||||
g.fluidsimidx = 1
|
||||
g.fluidsimidx = GameSims - 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,9 +264,9 @@ func (g *Game) ManageFluidSim10Inputs() {
|
||||
|
||||
func (g *Game) Initialize() {
|
||||
|
||||
//10MP Fluid Simulation Initialization
|
||||
g.fluidsim10 = append(g.fluidsim10, elements.NewFluidSim10())
|
||||
g.fluidsim10 = append(g.fluidsim10, elements.NewFluidSim10())
|
||||
//g.fluidsim10 = append(g.fluidsim10, elements.NewFluidSim10())
|
||||
|
||||
g.fluidsim10width = float64(g.fluidsim10[0].GetSprite().Bounds().Dx())
|
||||
g.fluidsim10height = float64(g.fluidsim10[0].GetSprite().Bounds().Dy())
|
||||
@@ -268,4 +282,81 @@ func (g *Game) Initialize() {
|
||||
sim.SetPosition(pos)
|
||||
}
|
||||
|
||||
//Flask Initialization
|
||||
pos := gamedata.Vector{
|
||||
X: GameWidth / 2,
|
||||
Y: GameHeight / 2,
|
||||
}
|
||||
g.flask.SetPosition(pos)
|
||||
g.flaskcolor = color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0xff}
|
||||
g.flask.SetFluidColor(g.flaskcolor)
|
||||
|
||||
}
|
||||
|
||||
func (g *Game) ManageFlaskInputs() {
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
|
||||
g.flask.Initialize()
|
||||
g.mfangle = 0
|
||||
g.flask.SetAngle(g.mfangle)
|
||||
g.flask.SetFluidColor(g.flaskcolor)
|
||||
}
|
||||
|
||||
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
||||
//angle at mouse down is the zero angle
|
||||
g.mdown = true
|
||||
g.mdx, g.mdy = ebiten.CursorPosition()
|
||||
|
||||
dx := float64(g.mdx) - g.flask.GetPosition().X
|
||||
dy := float64(g.mdy) - g.flask.GetPosition().Y
|
||||
g.mdangle = math.Atan2(dy, dx)
|
||||
}
|
||||
|
||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
||||
|
||||
if g.mdown {
|
||||
mx, my := ebiten.CursorPosition()
|
||||
|
||||
dx := float64(mx) - g.flask.GetPosition().X
|
||||
dy := float64(my) - g.flask.GetPosition().Y
|
||||
angle := math.Atan2(dy, dx)
|
||||
|
||||
g.flask.SetAngle(g.mfangle + (angle - g.mdangle))
|
||||
}
|
||||
}
|
||||
|
||||
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
|
||||
g.mdown = false
|
||||
|
||||
mx, my := ebiten.CursorPosition()
|
||||
|
||||
dx := float64(mx) - g.flask.GetPosition().X
|
||||
dy := float64(my) - g.flask.GetPosition().Y
|
||||
angle := math.Atan2(dy, dx)
|
||||
|
||||
g.mfangle = g.mfangle + (angle - g.mdangle)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) UpdateFlask() {
|
||||
g.flask.Update()
|
||||
}
|
||||
|
||||
func (g *Game) RenderFlask(img *ebiten.Image) {
|
||||
|
||||
g.flask.Draw()
|
||||
|
||||
angle := g.flask.GetAngle()
|
||||
dim := g.flask.GetDimensions()
|
||||
|
||||
// //TODO: use flask position for rendering, not screen
|
||||
pos := g.flask.GetPosition()
|
||||
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(-dim.X/2, -dim.Y/2)
|
||||
op.GeoM.Rotate(angle)
|
||||
op.GeoM.Scale(2, 2)
|
||||
op.GeoM.Translate(pos.X, pos.Y)
|
||||
|
||||
img.DrawImage(g.flask.GetSprite(), op)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user