ebiten/examples/airship/main.go

284 lines
7.2 KiB
Go
Raw Normal View History

2016-07-16 22:36:06 +02:00
// Copyright 2016 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2020-10-06 17:45:54 +02:00
// +build example
2016-07-16 22:36:06 +02:00
package main
import (
"bytes"
2016-07-16 22:36:06 +02:00
"fmt"
2016-07-17 22:19:22 +02:00
"image"
2016-07-16 22:36:06 +02:00
"image/color"
2016-07-27 18:10:40 +02:00
_ "image/jpeg"
2016-07-16 22:36:06 +02:00
"log"
"math"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
2016-07-16 22:36:06 +02:00
)
const (
screenWidth = 320
screenHeight = 240
maxAngle = 256
maxLean = 16
)
var (
2020-04-12 11:18:45 +02:00
skyColor = color.RGBA{0x66, 0xcc, 0xff, 0xff}
2018-01-30 18:12:35 +01:00
gophersImage *ebiten.Image
repeatedGophersImage *ebiten.Image
groundImage = ebiten.NewImage(screenWidth*2, screenHeight*2/3+50)
perspectiveGroundImage = ebiten.NewImage(screenWidth*2, screenHeight)
2018-01-30 18:12:35 +01:00
fogImage *ebiten.Image
2016-07-16 22:36:06 +02:00
)
2017-06-04 12:01:07 +02:00
func init() {
// Decode image from a byte slice instead of a file so that
// this example works in any working directory.
// If you want to use a file, there are some options:
// 1) Use os.Open and pass the file to the image decoder.
// This is a very regular way, but doesn't work on browsers.
// 2) Use ebitenutil.OpenFile and pass the file to the image decoder.
// This works even on browsers.
// 3) Use ebitenutil.NewImageFromFile to create an ebiten.Image directly from a file.
// This also works on browsers.
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
2017-06-04 12:01:07 +02:00
if err != nil {
log.Fatal(err)
2017-06-04 12:01:07 +02:00
}
gophersImage = ebiten.NewImageFromImage(img)
2017-06-04 12:01:07 +02:00
const repeat = 5
w, h := gophersImage.Size()
repeatedGophersImage = ebiten.NewImage(w*repeat, h*repeat)
2017-06-04 12:01:07 +02:00
for j := 0; j < repeat; j++ {
for i := 0; i < repeat; i++ {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(w*i), float64(h*j))
repeatedGophersImage.DrawImage(gophersImage, op)
}
}
const fogHeight = 8
2018-01-30 18:12:35 +01:00
w, _ = perspectiveGroundImage.Size()
fogRGBA := image.NewRGBA(image.Rect(0, 0, w, fogHeight))
2017-06-04 12:01:07 +02:00
for j := 0; j < fogHeight; j++ {
a := uint32(float64(fogHeight-1-j) * 0xff / (fogHeight - 1))
clr := skyColor
r, g, b, oa := uint32(clr.R), uint32(clr.G), uint32(clr.B), uint32(clr.A)
clr.R = uint8(r * a / oa)
clr.G = uint8(g * a / oa)
clr.B = uint8(b * a / oa)
clr.A = uint8(a)
2018-01-30 18:12:35 +01:00
for i := 0; i < w; i++ {
2017-06-04 12:01:07 +02:00
fogRGBA.SetRGBA(i, j, clr)
}
}
fogImage = ebiten.NewImageFromImage(fogRGBA)
2017-06-04 12:01:07 +02:00
}
2018-03-11 17:45:15 +01:00
// player represents the current airship's position.
2016-07-16 22:36:06 +02:00
type player struct {
2018-03-11 17:45:15 +01:00
// x16, y16 represents the position in XY plane in fixed float format.
// The fractional part has 16 bits of precision.
x16 int
y16 int
// angle represents the player's angle in XY plane.
// angle takes an integer value in [0, maxAngle).
2016-07-16 22:36:06 +02:00
angle int
2018-03-11 17:45:15 +01:00
// lean represents the player's leaning.
// lean takes an integer value in [-maxLean, maxLean].
lean int
2016-07-16 22:36:06 +02:00
}
func round(x float64) float64 {
return math.Floor(x + 0.5)
}
2018-03-11 17:45:15 +01:00
// MoveForward moves the player p forward.
2016-07-16 22:36:06 +02:00
func (p *player) MoveForward() {
w, h := gophersImage.Size()
mx := w * 16
my := h * 16
s, c := math.Sincos(float64(p.angle) * 2 * math.Pi / maxAngle)
2016-07-17 17:52:54 +02:00
p.x16 += int(round(16*c) * 2)
p.y16 += int(round(16*s) * 2)
2016-07-16 22:36:06 +02:00
for mx <= p.x16 {
p.x16 -= mx
}
for my <= p.y16 {
p.y16 -= my
}
for p.x16 < 0 {
p.x16 += mx
}
for p.y16 < 0 {
p.y16 += my
}
}
2018-03-11 17:45:15 +01:00
// RotateRight rotates the player p in the right direction.
2016-07-16 22:36:06 +02:00
func (p *player) RotateRight() {
p.angle++
if maxAngle <= p.angle {
p.angle -= maxAngle
}
p.lean++
if maxLean < p.lean {
p.lean = maxLean
}
}
2018-03-11 17:45:15 +01:00
// RotateLeft rotates the player p in the left direction.
2016-07-16 22:36:06 +02:00
func (p *player) RotateLeft() {
p.angle--
if p.angle < 0 {
p.angle += maxAngle
}
p.lean--
if p.lean < -maxLean {
p.lean = -maxLean
}
}
2018-03-11 17:45:15 +01:00
// Stabilize tries to move the player in the stable position (lean).
2016-07-16 22:36:06 +02:00
func (p *player) Stabilize() {
if 0 < p.lean {
p.lean--
}
if p.lean < 0 {
p.lean++
}
}
2018-03-11 17:45:15 +01:00
// Position returns the player p's position.
2016-07-16 22:36:06 +02:00
func (p *player) Position() (int, int) {
return p.x16, p.y16
}
2018-03-11 17:45:15 +01:00
// Angle returns the player p's angle.
2016-07-16 22:36:06 +02:00
func (p *player) Angle() int {
return p.angle
}
2018-03-11 17:45:15 +01:00
// updateGroundImage updates the ground image according to the current player's position.
2020-04-12 11:18:45 +02:00
func (g *Game) updateGroundImage(ground *ebiten.Image) {
ground.Clear()
2018-01-21 16:20:35 +01:00
2020-04-12 11:18:45 +02:00
x16, y16 := g.player.Position()
a := g.player.Angle()
2016-07-16 22:36:06 +02:00
gw, gh := ground.Size()
w, h := gophersImage.Size()
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(-x16)/16, float64(-y16)/16)
op.GeoM.Translate(float64(-w*2), float64(-h*2))
op.GeoM.Rotate(float64(-a)*2*math.Pi/maxAngle + math.Pi*3.0/2.0)
op.GeoM.Translate(float64(gw)/2, float64(gh)-32)
ground.DrawImage(repeatedGophersImage, op)
2016-07-16 22:36:06 +02:00
}
2018-03-11 17:45:15 +01:00
// drawGroundImage draws the ground image to the given screen image.
2020-04-12 11:18:45 +02:00
func (g *Game) drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
2018-01-30 18:12:35 +01:00
perspectiveGroundImage.Clear()
gw, _ := ground.Size()
2018-01-30 18:12:35 +01:00
pw, ph := perspectiveGroundImage.Size()
for j := 0; j < ph; j++ {
2018-03-14 17:17:07 +01:00
// z is in [2, -1]
rate := float64(j) / float64(ph)
2018-03-14 17:17:07 +01:00
z := (1-rate)*2 + rate*-1
// Avoid too small z, or the scale (1/z) can be too big.
if z <= 0.1 {
break
}
2018-01-30 18:12:35 +01:00
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(pw)/2, 0)
2018-03-11 19:18:07 +01:00
op.GeoM.Scale(1/z, 8) // 8 is an arbitrary number not to make empty lines.
op.GeoM.Translate(float64(pw)/2, float64(j)/z)
2017-05-27 19:24:23 +02:00
perspectiveGroundImage.DrawImage(ground.SubImage(image.Rect(0, j, gw, j+1)).(*ebiten.Image), op)
2017-05-27 19:24:23 +02:00
}
2018-01-30 18:12:35 +01:00
perspectiveGroundImage.DrawImage(fogImage, nil)
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(pw)/2, 0)
2020-04-12 11:18:45 +02:00
op.GeoM.Rotate(-1 * float64(g.player.lean) / maxLean * math.Pi / 8)
2018-01-30 18:12:35 +01:00
op.GeoM.Translate(float64(screenWidth)/2, screenHeight/3)
screen.DrawImage(perspectiveGroundImage, op)
2016-07-16 22:36:06 +02:00
}
2020-04-12 11:18:45 +02:00
type Game struct {
player *player
}
func NewGame() *Game {
return &Game{
player: &player{
x16: 16 * 100,
y16: 16 * 200,
angle: maxAngle * 3 / 4,
},
}
}
func (g *Game) Update() error {
2018-03-11 17:45:15 +01:00
// Manipulate the player by the input.
2016-07-16 22:36:06 +02:00
if ebiten.IsKeyPressed(ebiten.KeySpace) {
2020-04-12 11:18:45 +02:00
g.player.MoveForward()
2016-07-16 22:36:06 +02:00
}
rotated := false
if ebiten.IsKeyPressed(ebiten.KeyRight) {
2020-04-12 11:18:45 +02:00
g.player.RotateRight()
2016-07-16 22:36:06 +02:00
rotated = true
}
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
2020-04-12 11:18:45 +02:00
g.player.RotateLeft()
2016-07-16 22:36:06 +02:00
rotated = true
}
if !rotated {
2020-04-12 11:18:45 +02:00
g.player.Stabilize()
2017-05-16 03:35:58 +02:00
}
2020-04-12 11:18:45 +02:00
return nil
}
2018-01-21 15:51:56 +01:00
2020-04-12 11:18:45 +02:00
func (g *Game) Draw(screen *ebiten.Image) {
2018-03-11 17:45:15 +01:00
// Draw the ground image.
screen.Fill(skyColor)
2020-04-12 11:18:45 +02:00
g.updateGroundImage(groundImage)
g.drawGroundImage(screen, groundImage)
2018-03-11 17:45:15 +01:00
// Draw the message.
2016-08-01 18:49:05 +02:00
tutrial := "Space: Move forward\nLeft/Right: Rotate"
2019-02-12 05:03:32 +01:00
msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f\n%s", ebiten.CurrentTPS(), ebiten.CurrentFPS(), tutrial)
ebitenutil.DebugPrint(screen, msg)
2020-04-12 11:18:45 +02:00
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
2016-07-16 22:36:06 +02:00
}
func main() {
2020-04-12 11:18:45 +02:00
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Air Ship (Ebiten Demo)")
if err := ebiten.RunGame(NewGame()); err != nil {
2016-07-16 22:36:06 +02:00
log.Fatal(err)
}
}