Browse Source

FEATURE: Removed fancy physics which didnt work on web and added a simple drag and drop mechanic to its place

master
parent
commit
96bcf46dbb
  1. 2
      src/game/capybara.go
  2. 28
      src/game/game.go
  3. 68
      src/game/mandarinRain.go
  4. 2
      src/game/sprite.go
  5. 80
      src/game/stroke.go
  6. 2
      src/main.go

2
src/game/capybara.go

@ -73,7 +73,7 @@ func (c *Capybara) Draw(screen *ebiten.Image, level uint32) {
op := &ebiten.DrawImageOptions{}
capybaraBounds := c.Sprite.Img.Bounds()
scale := float64(screen.Bounds().Dx()) / float64(capybaraBounds.Dx()) / 2.5
scale := float64(screen.Bounds().Dx()) / float64(capybaraBounds.Dx()) / 2.0
c.Sprite.Scale = scale
op.GeoM.Scale(
scale+c.Sprite.Animation.Squish,

28
src/game/game.go

@ -44,6 +44,8 @@ type Game struct {
FontFace font.Face
PassiveIncomeTicker int
Screen *ebiten.Image
TouchIDs []ebiten.TouchID
Strokes map[*Stroke]struct{}
Capybara *Capybara
Background *Sprite
MandarinRain *MandarinRain
@ -74,6 +76,8 @@ func NewGame() Game {
DPI: 72,
Hinting: font.HintingVertical,
}),
TouchIDs: nil,
Strokes: map[*Stroke]struct{}{},
PassiveIncomeTicker: 0,
MandarinRain: NewMandarinRain(3, 8),
}
@ -165,6 +169,30 @@ func (g *Game) Update() error {
g.MandarinRain = NewMandarinRain(3, 8)
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) && g.MandarinRain.InProgress {
physical := g.MandarinRain.PhysicalAt(ebiten.CursorPosition())
if physical != nil {
s := NewStroke(&MouseStrokeSource{}, physical)
g.Strokes[s] = struct{}{}
}
}
g.TouchIDs = inpututil.AppendJustPressedTouchIDs(g.TouchIDs[:0])
for _, id := range g.TouchIDs {
physical := g.MandarinRain.PhysicalAt(ebiten.TouchPosition(id))
if physical != nil {
s := NewStroke(&TouchStrokeSource{id}, physical)
g.Strokes[s] = struct{}{}
}
}
for s := range g.Strokes {
s.Update(g)
if !s.Physical().Sprite.Dragged {
delete(g.Strokes, s)
}
}
return nil
}

68
src/game/mandarinRain.go

@ -22,7 +22,6 @@ import (
"math/rand"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type MandarinRain struct {
@ -35,12 +34,10 @@ type MandarinRain struct {
mandarinsInBox uint16
boxFull bool
mandarinCountRange [2]uint16
// screenBounds image.Rectangle
}
func NewMandarinRain(from uint16, to uint16) *MandarinRain {
rain := MandarinRain{}
// rain.screenBounds = WindowBounds()
rain.InProgress = false
rain.mandarinInitialCount = uint16(rand.Int31n(int32(to-from)) + int32(from))
rain.mandarinCountRange = [2]uint16{from, to}
@ -59,6 +56,20 @@ func NewMandarinRain(from uint16, to uint16) *MandarinRain {
return &rain
}
func (mr *MandarinRain) PhysicalAt(x int, y int) *Physical {
for _, orange := range mr.Mandarins {
if orange.Sprite.IsIn(x, y) {
return orange
}
}
if mr.MandarinBox.Sprite.IsIn(x, y) {
return mr.MandarinBox
}
return nil
}
func (mr *MandarinRain) Run(game *Game) {
if mr.InProgress {
return
@ -79,41 +90,12 @@ func (mr *MandarinRain) Run(game *Game) {
}
func (mr *MandarinRain) Update(game *Game) {
cPosX, cPosY := ebiten.CursorPosition()
var tPosX int = 0
var tPosY int = 0
if len(ebiten.AppendTouchIDs(nil)) != 0 {
tPosX, tPosY = ebiten.TouchPosition(ebiten.AppendTouchIDs(nil)[0])
}
// Oranges
temp := mr.Mandarins[:0]
for _, orange := range mr.Mandarins {
orange.Acceleration.Vx = 0.0
orange.Acceleration.Vy = 9.81 / orange.Mass
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) &&
orange.InVicinity(float64(cPosX), float64(cPosY), 75.0) {
difference := newVec2f(
(float64(cPosX-orange.Sprite.RealBounds().Dx()/2)-orange.Sprite.X)*4.5,
(float64(cPosY-orange.Sprite.RealBounds().Dy()/2)-orange.Sprite.Y)*4.5,
)
orange.Acceleration.Vx = difference.Vx / orange.Mass
orange.Acceleration.Vy = difference.Vy / orange.Mass
} else if len(inpututil.AppendJustPressedTouchIDs(nil)) != 0 &&
orange.InVicinity(float64(tPosX), float64(tPosY), 75.0) {
tPosX, tPosY := ebiten.TouchPosition(ebiten.AppendTouchIDs(nil)[0])
difference := newVec2f(
(float64(tPosX-orange.Sprite.RealBounds().Dx()/2)-orange.Sprite.X)*4.5,
(float64(tPosY-orange.Sprite.RealBounds().Dy()/2)-orange.Sprite.Y)*4.5,
)
orange.Acceleration.Vx = difference.Vx / orange.Mass
orange.Acceleration.Vy = difference.Vy / orange.Mass
}
orange.Velocity.Vx = orange.Velocity.Vx + orange.Acceleration.Vx*0.05
orange.Velocity.Vy = orange.Velocity.Vy + orange.Acceleration.Vy*0.05
@ -166,28 +148,6 @@ func (mr *MandarinRain) Update(game *Game) {
mr.MandarinBox.Acceleration.Vx = 0.0
mr.MandarinBox.Acceleration.Vy = 9.81 / mr.MandarinBox.Mass
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) &&
mr.MandarinBox.InVicinity(float64(cPosX), float64(cPosY), 75.0) {
difference := newVec2f(
(float64(cPosX-mr.MandarinBox.Sprite.RealBounds().Dx()/2)-mr.MandarinBox.Sprite.X)*3.5,
(float64(cPosY-mr.MandarinBox.Sprite.RealBounds().Dy()/2)-mr.MandarinBox.Sprite.Y)*3.5,
)
mr.MandarinBox.Acceleration.Vx = difference.Vx / mr.MandarinBox.Mass
mr.MandarinBox.Acceleration.Vy = difference.Vy / mr.MandarinBox.Mass
} else if len(inpututil.AppendJustPressedTouchIDs(nil)) != 0 &&
mr.MandarinBox.InVicinity(float64(tPosX), float64(tPosY), 75.0) {
tPosX, tPosY := ebiten.TouchPosition(ebiten.AppendTouchIDs(nil)[0])
difference := newVec2f(
(float64(tPosX-mr.MandarinBox.Sprite.RealBounds().Dx()/2)-mr.MandarinBox.Sprite.X)*3.5,
(float64(tPosY-mr.MandarinBox.Sprite.RealBounds().Dy()/2)-mr.MandarinBox.Sprite.Y)*3.5,
)
mr.MandarinBox.Acceleration.Vx = difference.Vx / mr.MandarinBox.Mass
mr.MandarinBox.Acceleration.Vy = difference.Vy / mr.MandarinBox.Mass
}
mr.MandarinBox.Velocity.Vx = mr.MandarinBox.Velocity.Vx + mr.MandarinBox.Acceleration.Vx*0.05
mr.MandarinBox.Velocity.Vy = mr.MandarinBox.Velocity.Vy + mr.MandarinBox.Acceleration.Vy*0.05

2
src/game/sprite.go

@ -38,6 +38,7 @@ type Sprite struct {
Y float64
Animation AnimationData
Scale float64
Dragged bool
}
func NewSprite(img image.Image) *Sprite {
@ -51,6 +52,7 @@ func NewSprite(img image.Image) *Sprite {
BounceDirectionFlag: false,
},
Scale: 1.0,
Dragged: false,
}
}

80
src/game/stroke.go

@ -0,0 +1,80 @@
package game
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type StrokeSource interface {
Position() (int, int)
IsJustReleased() bool
}
type MouseStrokeSource struct{}
func (m *MouseStrokeSource) Position() (int, int) {
return ebiten.CursorPosition()
}
func (m *MouseStrokeSource) IsJustReleased() bool {
return inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft)
}
type TouchStrokeSource struct {
ID ebiten.TouchID
}
func (t *TouchStrokeSource) Position() (int, int) {
return ebiten.TouchPosition(t.ID)
}
func (t *TouchStrokeSource) IsJustReleased() bool {
return inpututil.IsTouchJustReleased(t.ID)
}
type Stroke struct {
source StrokeSource
offsetX float64
offsetY float64
physical *Physical
}
func NewStroke(source StrokeSource, physical *Physical) *Stroke {
physical.Sprite.Dragged = true
x, y := source.Position()
return &Stroke{
source: source,
offsetX: float64(x) - physical.Sprite.X,
offsetY: float64(y) - physical.Sprite.Y,
physical: physical,
}
}
func (s *Stroke) Update(game *Game) {
if !s.physical.Sprite.Dragged {
return
}
// xp, yp := s.source.Position()
// difference := newVec2f(
// (float64(xp-s.physical.Sprite.RealBounds().Dx()/2)-s.physical.Sprite.X)*3.5,
// (float64(yp-s.physical.Sprite.RealBounds().Dy()/2)-s.physical.Sprite.Y)*3.5,
// )
// s.physical.Acceleration.Vx = difference.Vx / s.physical.Mass
// s.physical.Acceleration.Vy = difference.Vy / s.physical.Mass
if s.source.IsJustReleased() {
s.physical.Sprite.Dragged = false
return
}
ix, iy := s.source.Position()
x := float64(ix) - s.offsetX
y := float64(iy) - s.offsetY
s.physical.Sprite.MoveTo(x, y, game.Screen)
}
func (s *Stroke) Physical() *Physical {
return s.physical
}

2
src/main.go

@ -36,7 +36,7 @@ import (
"github.com/hajimehoshi/ebiten/v2"
)
const Version string = "v0.1.1-release"
const Version string = "v0.1.2-release"
var (
silent *bool = flag.Bool("silent", false, "Set to true in order to discard all logging")

Loading…
Cancel
Save