1
0

main.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "log"
  17. "time"
  18. "github.com/hajimehoshi/ebiten/v2"
  19. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  20. "github.com/hajimehoshi/ebiten/v2/inpututil"
  21. )
  22. const (
  23. screenWidth = 640
  24. screenHeight = 480
  25. )
  26. type Game struct {
  27. touchIDs []ebiten.TouchID
  28. gamepadIDs []ebiten.GamepadID
  29. touchCounter int
  30. }
  31. func (g *Game) Update() error {
  32. g.touchIDs = inpututil.AppendJustPressedTouchIDs(g.touchIDs[:0])
  33. if len(g.touchIDs) > 0 {
  34. g.touchCounter++
  35. op := &ebiten.VibrateOptions{
  36. Duration: 200 * time.Millisecond,
  37. Magnitude: 0.5*float64(g.touchCounter%2) + 0.5,
  38. }
  39. ebiten.Vibrate(op)
  40. }
  41. g.gamepadIDs = g.gamepadIDs[:0]
  42. g.gamepadIDs = ebiten.AppendGamepadIDs(g.gamepadIDs)
  43. for _, id := range g.gamepadIDs {
  44. for b := ebiten.GamepadButton0; b <= ebiten.GamepadButtonMax; b++ {
  45. if !inpututil.IsGamepadButtonJustPressed(id, b) {
  46. continue
  47. }
  48. // TODO: Test weak-magnitude.
  49. op := &ebiten.VibrateGamepadOptions{
  50. Duration: 200 * time.Millisecond,
  51. StrongMagnitude: 1,
  52. WeakMagnitude: 0,
  53. }
  54. ebiten.VibrateGamepad(id, op)
  55. break
  56. }
  57. }
  58. return nil
  59. }
  60. func (g *Game) Draw(screen *ebiten.Image) {
  61. msg := "Touch the screen to vibrate the screen."
  62. if len(g.gamepadIDs) > 0 {
  63. msg += "\nPress a gamepad button to vibrate the gamepad."
  64. }
  65. ebitenutil.DebugPrint(screen, msg)
  66. }
  67. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  68. return screenWidth, screenHeight
  69. }
  70. func main() {
  71. ebiten.SetWindowTitle("Vibrate (Ebitengine Demo)")
  72. if err := ebiten.RunGame(&Game{}); err != nil {
  73. log.Fatal(err)
  74. }
  75. }