game.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2021 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "math"
  18. "github.com/hajimehoshi/ebiten/v2"
  19. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  20. "github.com/hajimehoshi/ebiten/v2/inpututil"
  21. )
  22. // Game is an isometric demo game.
  23. type Game struct {
  24. w, h int
  25. currentLevel *Level
  26. camX, camY float64
  27. camScale float64
  28. camScaleTo float64
  29. mousePanX, mousePanY int
  30. offscreen *ebiten.Image
  31. }
  32. // NewGame returns a new isometric demo Game.
  33. func NewGame() (*Game, error) {
  34. l, err := NewLevel()
  35. if err != nil {
  36. return nil, fmt.Errorf("failed to create new level: %s", err)
  37. }
  38. g := &Game{
  39. currentLevel: l,
  40. camScale: 1,
  41. camScaleTo: 1,
  42. mousePanX: math.MinInt32,
  43. mousePanY: math.MinInt32,
  44. }
  45. return g, nil
  46. }
  47. // Update reads current user input and updates the Game state.
  48. func (g *Game) Update() error {
  49. // Update target zoom level.
  50. var scrollY float64
  51. if ebiten.IsKeyPressed(ebiten.KeyC) || ebiten.IsKeyPressed(ebiten.KeyPageDown) {
  52. scrollY = -0.25
  53. } else if ebiten.IsKeyPressed(ebiten.KeyE) || ebiten.IsKeyPressed(ebiten.KeyPageUp) {
  54. scrollY = .25
  55. } else {
  56. _, scrollY = ebiten.Wheel()
  57. if scrollY < -1 {
  58. scrollY = -1
  59. } else if scrollY > 1 {
  60. scrollY = 1
  61. }
  62. }
  63. g.camScaleTo += scrollY * (g.camScaleTo / 7)
  64. // Clamp target zoom level.
  65. if g.camScaleTo < 0.01 {
  66. g.camScaleTo = 0.01
  67. } else if g.camScaleTo > 100 {
  68. g.camScaleTo = 100
  69. }
  70. // Smooth zoom transition.
  71. div := 10.0
  72. if g.camScaleTo > g.camScale {
  73. g.camScale += (g.camScaleTo - g.camScale) / div
  74. } else if g.camScaleTo < g.camScale {
  75. g.camScale -= (g.camScale - g.camScaleTo) / div
  76. }
  77. // Pan camera via keyboard.
  78. pan := 7.0 / g.camScale
  79. if ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyA) {
  80. g.camX -= pan
  81. }
  82. if ebiten.IsKeyPressed(ebiten.KeyRight) || ebiten.IsKeyPressed(ebiten.KeyD) {
  83. g.camX += pan
  84. }
  85. if ebiten.IsKeyPressed(ebiten.KeyDown) || ebiten.IsKeyPressed(ebiten.KeyS) {
  86. g.camY -= pan
  87. }
  88. if ebiten.IsKeyPressed(ebiten.KeyUp) || ebiten.IsKeyPressed(ebiten.KeyW) {
  89. g.camY += pan
  90. }
  91. // Pan camera via mouse.
  92. if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
  93. if g.mousePanX == math.MinInt32 && g.mousePanY == math.MinInt32 {
  94. g.mousePanX, g.mousePanY = ebiten.CursorPosition()
  95. } else {
  96. x, y := ebiten.CursorPosition()
  97. dx, dy := float64(g.mousePanX-x)*(pan/100), float64(g.mousePanY-y)*(pan/100)
  98. g.camX, g.camY = g.camX-dx, g.camY+dy
  99. }
  100. } else if g.mousePanX != math.MinInt32 || g.mousePanY != math.MinInt32 {
  101. g.mousePanX, g.mousePanY = math.MinInt32, math.MinInt32
  102. }
  103. // Clamp camera position.
  104. worldWidth := float64(g.currentLevel.w * g.currentLevel.tileSize / 2)
  105. worldHeight := float64(g.currentLevel.h * g.currentLevel.tileSize / 2)
  106. if g.camX < -worldWidth {
  107. g.camX = -worldWidth
  108. } else if g.camX > worldWidth {
  109. g.camX = worldWidth
  110. }
  111. if g.camY < -worldHeight {
  112. g.camY = -worldHeight
  113. } else if g.camY > 0 {
  114. g.camY = 0
  115. }
  116. // Randomize level.
  117. if inpututil.IsKeyJustPressed(ebiten.KeyR) {
  118. l, err := NewLevel()
  119. if err != nil {
  120. return fmt.Errorf("failed to create new level: %s", err)
  121. }
  122. g.currentLevel = l
  123. }
  124. return nil
  125. }
  126. // Draw draws the Game on the screen.
  127. func (g *Game) Draw(screen *ebiten.Image) {
  128. // Render level.
  129. g.renderLevel(screen)
  130. // Print game info.
  131. ebitenutil.DebugPrint(screen, fmt.Sprintf("KEYS WASD EC R\nFPS %0.0f\nTPS %0.0f\nSCA %0.2f\nPOS %0.0f,%0.0f", ebiten.ActualFPS(), ebiten.ActualTPS(), g.camScale, g.camX, g.camY))
  132. }
  133. // Layout is called when the Game's layout changes.
  134. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  135. g.w, g.h = outsideWidth, outsideHeight
  136. return g.w, g.h
  137. }
  138. // cartesianToIso transforms cartesian coordinates into isometric coordinates.
  139. func (g *Game) cartesianToIso(x, y float64) (float64, float64) {
  140. tileSize := g.currentLevel.tileSize
  141. ix := (x - y) * float64(tileSize/2)
  142. iy := (x + y) * float64(tileSize/4)
  143. return ix, iy
  144. }
  145. /*
  146. This function might be useful for those who want to modify this example.
  147. // isoToCartesian transforms isometric coordinates into cartesian coordinates.
  148. func (g *Game) isoToCartesian(x, y float64) (float64, float64) {
  149. tileSize := g.currentLevel.tileSize
  150. cx := (x/float64(tileSize/2) + y/float64(tileSize/4)) / 2
  151. cy := (y/float64(tileSize/4) - (x / float64(tileSize/2))) / 2
  152. return cx, cy
  153. }
  154. */
  155. // renderLevel draws the current Level on the screen.
  156. func (g *Game) renderLevel(screen *ebiten.Image) {
  157. op := &ebiten.DrawImageOptions{}
  158. padding := float64(g.currentLevel.tileSize) * g.camScale
  159. cx, cy := float64(g.w/2), float64(g.h/2)
  160. scaleLater := g.camScale > 1
  161. target := screen
  162. scale := g.camScale
  163. // When zooming in, tiles can have slight bleeding edges.
  164. // To avoid them, render the result on an offscreen first and then scale it later.
  165. if scaleLater {
  166. if g.offscreen != nil {
  167. if g.offscreen.Bounds().Size() != screen.Bounds().Size() {
  168. g.offscreen.Deallocate()
  169. g.offscreen = nil
  170. }
  171. }
  172. if g.offscreen == nil {
  173. s := screen.Bounds().Size()
  174. g.offscreen = ebiten.NewImage(s.X, s.Y)
  175. }
  176. target = g.offscreen
  177. target.Clear()
  178. scale = 1
  179. }
  180. for y := 0; y < g.currentLevel.h; y++ {
  181. for x := 0; x < g.currentLevel.w; x++ {
  182. xi, yi := g.cartesianToIso(float64(x), float64(y))
  183. // Skip drawing tiles that are out of the screen.
  184. drawX, drawY := ((xi-g.camX)*g.camScale)+cx, ((yi+g.camY)*g.camScale)+cy
  185. if drawX+padding < 0 || drawY+padding < 0 || drawX > float64(g.w) || drawY > float64(g.h) {
  186. continue
  187. }
  188. t := g.currentLevel.tiles[y][x]
  189. if t == nil {
  190. continue // No tile at this position.
  191. }
  192. op.GeoM.Reset()
  193. // Move to current isometric position.
  194. op.GeoM.Translate(xi, yi)
  195. // Translate camera position.
  196. op.GeoM.Translate(-g.camX, g.camY)
  197. // Zoom.
  198. op.GeoM.Scale(scale, scale)
  199. // Center.
  200. op.GeoM.Translate(cx, cy)
  201. t.Draw(target, op)
  202. }
  203. }
  204. if scaleLater {
  205. op := &ebiten.DrawImageOptions{}
  206. op.GeoM.Translate(-cx, -cy)
  207. op.GeoM.Scale(float64(g.camScale), float64(g.camScale))
  208. op.GeoM.Translate(cx, cy)
  209. screen.DrawImage(target, op)
  210. }
  211. }