Skip to content

Commit

Permalink
Added: SetCenter()
Browse files Browse the repository at this point in the history
  • Loading branch information
setanarut committed Jan 17, 2025
1 parent 69a987b commit bc91a23
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
16 changes: 13 additions & 3 deletions camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,21 @@ func (cam *Camera) Bottom() float64 {

// SetTopLeft sets top-left position of the camera in world-space.
//
// Unlike the LookAt() method, the position is set directly without any smoothing.
//
// Useful for static cameras.
// Unlike the LookAt() method, the position is set directly (teleport).
func (cam *Camera) SetTopLeft(x, y float64) {
cam.TopLeftX, cam.TopLeftY = x, y
cam.tempTargetX, cam.tempTargetY = cam.Center()

}

// SetCenter sets center position of the camera in world-space.
//
// Unlike the LookAt() method, the position is set directly (teleport).
//
// Can be used to cancel follow camera and teleport to target.
func (cam *Camera) SetCenter(x, y float64) {
cam.LookAt(x, y)
cam.tempTargetX, cam.tempTargetY = x, y
}

// Center returns center point of the camera in world-space
Expand Down
3 changes: 3 additions & 0 deletions examples/director/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ func (g *Game) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyTab) {
switch cam.SmoothType {
case kamera.None:
cam.SetCenter(targetX, targetY)
cam.SmoothType = kamera.Lerp
case kamera.Lerp:
cam.SetCenter(targetX, targetY)
cam.SmoothType = kamera.SmoothDamp
case kamera.SmoothDamp:
cam.SetCenter(targetX, targetY)
cam.SmoothType = kamera.None
}
}
Expand Down
3 changes: 3 additions & 0 deletions examples/director_colorm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ func (g *Game) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyTab) {
switch cam.SmoothType {
case kamera.None:
cam.SetCenter(targetX, targetY)
cam.SmoothType = kamera.Lerp
case kamera.Lerp:
cam.SetCenter(targetX, targetY)
cam.SmoothType = kamera.SmoothDamp
case kamera.SmoothDamp:
cam.SetCenter(targetX, targetY)
cam.SmoothType = kamera.None
}
}
Expand Down
3 changes: 3 additions & 0 deletions examples/platformer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ func (g *Game) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyTab) {
switch cam.SmoothType {
case kamera.None:
cam.SetCenter(Box[0]+Box[2]/2, Box[1]+Box[3]/2)
cam.SmoothType = kamera.Lerp
case kamera.Lerp:
cam.SetCenter(Box[0]+Box[2]/2, Box[1]+Box[3]/2)
cam.SmoothType = kamera.SmoothDamp
case kamera.SmoothDamp:
cam.SetCenter(Box[0]+Box[2]/2, Box[1]+Box[3]/2)
cam.SmoothType = kamera.None
}
}
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ module github.com/setanarut/kamera/v2

go 1.23.4

require github.com/hajimehoshi/ebiten/v2 v2.8.5
require github.com/hajimehoshi/ebiten/v2 v2.8.6

require (
github.com/ebitengine/gomobile v0.0.0-20241016134836-cc2e38a7c0ee // indirect
github.com/ebitengine/hideconsole v1.0.0 // indirect
github.com/ebitengine/purego v0.8.1 // indirect
github.com/ebitengine/purego v0.8.2 // indirect
github.com/jezek/xgb v1.1.1 // indirect
github.com/setanarut/fastnoise v1.1.1
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.29.0 // indirect
)

require github.com/setanarut/tilecollider v1.4.6
require github.com/setanarut/tilecollider v1.4.7

require golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
require golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 // indirect

retract (
v2.6.0 // nil pointer dereference (jpg) in demo code.
v2.93.0 // GeoM.Reset() bug.
v2.6.0 // nil pointer dereference (jpg) in demo code.
[v2.0.0, v2.2.0]
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@ github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
github.com/ebitengine/purego v0.8.1 h1:sdRKd6plj7KYW33EH5As6YKfe8m9zbN9JMrOjNVF/BE=
github.com/ebitengine/purego v0.8.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/ebitengine/purego v0.8.2 h1:jPPGWs2sZ1UgOSgD2bClL0MJIqu58nOmIcBuXr62z1I=
github.com/ebitengine/purego v0.8.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/hajimehoshi/ebiten/v2 v2.8.5 h1:w1/3XxjEwIo+amtQCOnCrwGzu4e6dr0ewu83JUKoxrM=
github.com/hajimehoshi/ebiten/v2 v2.8.5/go.mod h1:SXx/whkvpfsavGo6lvZykprerakl+8Uo1X8d2U5aAnA=
github.com/hajimehoshi/ebiten/v2 v2.8.6 h1:Dkd/sYI0TYyZRCE7GVxV59XC+WCi2BbGAbIBjXeVC1U=
github.com/hajimehoshi/ebiten/v2 v2.8.6/go.mod h1:cCQ3np7rdmaJa1ZnvslraVlpxNb3wCjEnAP1LHNyXNA=
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/setanarut/fastnoise v1.1.1 h1:cD9gUjY9GMxVab+B7AY09j2GAHMdiKzchDE5z6dQ7eE=
github.com/setanarut/fastnoise v1.1.1/go.mod h1:74vG3/RcPPcNi2M0riHJXQp9/+eTSh0rnQ/0WTEY6SU=
github.com/setanarut/tilecollider v1.4.6 h1:q85kxl3/ZXv+uBBSUKcCQWFB55qraXl8q9rWa/nVWP8=
github.com/setanarut/tilecollider v1.4.6/go.mod h1:Jw1lRLQAH+cVgUcCl1izFaSXctjiPHkJHd3uIftFdbU=
github.com/setanarut/tilecollider v1.4.7 h1:+m8mHTyklCfdkaYfF6R8sVk1e0jWxNWp8V421mis1Xw=
github.com/setanarut/tilecollider v1.4.7/go.mod h1:Jw1lRLQAH+cVgUcCl1izFaSXctjiPHkJHd3uIftFdbU=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 h1:yqrTHse8TCMW1M1ZCP+VAR/l0kKxwaAIqN/il7x4voA=
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

0 comments on commit bc91a23

Please sign in to comment.