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.
|
|
|
|
|
2016-08-25 17:40:39 +02:00
|
|
|
// +build example
|
|
|
|
|
2016-07-16 22:36:06 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 320
|
|
|
|
screenHeight = 240
|
|
|
|
maxAngle = 256
|
|
|
|
maxLean = 16
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-07-16 23:56:10 +02:00
|
|
|
skyColor = color.RGBA{0x66, 0xcc, 0xff, 0xff}
|
|
|
|
thePlayer = &player{
|
|
|
|
x16: 16 * 100,
|
|
|
|
y16: 16 * 200,
|
|
|
|
angle: maxAngle * 3 / 4,
|
|
|
|
}
|
2016-07-18 10:44:25 +02:00
|
|
|
gophersImage *ebiten.Image
|
|
|
|
repeatedGophersImage *ebiten.Image
|
|
|
|
groundImage *ebiten.Image
|
|
|
|
fogImage *ebiten.Image
|
2016-07-16 22:36:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type player struct {
|
|
|
|
x16 int
|
|
|
|
y16 int
|
|
|
|
angle int
|
|
|
|
lean int
|
|
|
|
}
|
|
|
|
|
|
|
|
func round(x float64) float64 {
|
|
|
|
return math.Floor(x + 0.5)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) RotateRight() {
|
|
|
|
p.angle++
|
|
|
|
if maxAngle <= p.angle {
|
|
|
|
p.angle -= maxAngle
|
|
|
|
}
|
|
|
|
p.lean++
|
|
|
|
if maxLean < p.lean {
|
|
|
|
p.lean = maxLean
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) RotateLeft() {
|
|
|
|
p.angle--
|
|
|
|
if p.angle < 0 {
|
|
|
|
p.angle += maxAngle
|
|
|
|
}
|
|
|
|
p.lean--
|
|
|
|
if p.lean < -maxLean {
|
|
|
|
p.lean = -maxLean
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Stabilize() {
|
|
|
|
if 0 < p.lean {
|
|
|
|
p.lean--
|
|
|
|
}
|
|
|
|
if p.lean < 0 {
|
|
|
|
p.lean++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Position() (int, int) {
|
|
|
|
return p.x16, p.y16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Angle() int {
|
|
|
|
return p.angle
|
|
|
|
}
|
|
|
|
|
2017-03-04 15:00:19 +01:00
|
|
|
func updateGroundImage(ground *ebiten.Image) {
|
|
|
|
ground.Clear()
|
2016-07-16 22:36:06 +02:00
|
|
|
x16, y16 := thePlayer.Position()
|
|
|
|
a := thePlayer.Angle()
|
|
|
|
gw, gh := ground.Size()
|
|
|
|
w, h := gophersImage.Size()
|
2016-07-18 10:44:25 +02:00
|
|
|
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)
|
2017-03-04 15:00:19 +01:00
|
|
|
ground.DrawImage(repeatedGophersImage, op)
|
2016-07-16 22:36:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type groundParts struct {
|
|
|
|
image *ebiten.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *groundParts) Len() int {
|
|
|
|
_, h := g.image.Size()
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *groundParts) Src(i int) (int, int, int, int) {
|
|
|
|
w, _ := g.image.Size()
|
2016-07-16 23:56:10 +02:00
|
|
|
return 0, i, w, i + 1
|
2016-07-16 22:36:06 +02:00
|
|
|
}
|
|
|
|
|
2016-07-17 17:52:54 +02:00
|
|
|
func (g *groundParts) scaleForLine(x float64) float64 {
|
|
|
|
_, h := g.image.Size()
|
|
|
|
x = float64(h) - x
|
|
|
|
return 200*((-x+50)/(x+50)+1) - 200*((-float64(h)+50)/(float64(h)+50)+1)
|
|
|
|
}
|
|
|
|
|
2016-07-16 22:36:06 +02:00
|
|
|
func (g *groundParts) Dst(i int) (int, int, int, int) {
|
|
|
|
w, _ := g.image.Size()
|
2016-07-17 17:52:54 +02:00
|
|
|
r := g.scaleForLine(float64(i))
|
|
|
|
j1 := g.scaleForLine(float64(i))
|
|
|
|
j2 := g.scaleForLine(float64(i + 1))
|
|
|
|
return -int(r), int(j1), w + int(r), int(math.Ceil(j2))
|
2016-07-16 22:36:06 +02:00
|
|
|
}
|
|
|
|
|
2017-03-04 15:00:19 +01:00
|
|
|
func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
|
2016-07-16 22:36:06 +02:00
|
|
|
w, _ := ground.Size()
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
op.GeoM.Translate(-float64(w)/2, 0)
|
|
|
|
op.GeoM.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8)
|
|
|
|
op.GeoM.Translate(float64(w)/2, 0)
|
2016-07-17 08:20:57 +02:00
|
|
|
op.GeoM.Translate(float64(screenWidth-w)/2, screenHeight/3)
|
2016-07-16 22:36:06 +02:00
|
|
|
op.ImageParts = &groundParts{ground}
|
2017-03-04 15:00:19 +01:00
|
|
|
screen.DrawImage(ground, op)
|
2016-07-17 22:19:22 +02:00
|
|
|
op = &ebiten.DrawImageOptions{}
|
|
|
|
op.GeoM.Translate(-float64(w)/2, 0)
|
|
|
|
op.GeoM.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8)
|
|
|
|
op.GeoM.Translate(float64(w)/2, 0)
|
|
|
|
op.GeoM.Translate(float64(screenWidth-w)/2, screenHeight/3)
|
2017-03-04 15:00:19 +01:00
|
|
|
screen.DrawImage(fogImage, op)
|
2016-07-16 22:36:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func update(screen *ebiten.Image) error {
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeySpace) {
|
|
|
|
thePlayer.MoveForward()
|
|
|
|
}
|
|
|
|
rotated := false
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyRight) {
|
|
|
|
thePlayer.RotateRight()
|
|
|
|
rotated = true
|
|
|
|
}
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
|
|
|
|
thePlayer.RotateLeft()
|
|
|
|
rotated = true
|
|
|
|
}
|
|
|
|
if !rotated {
|
|
|
|
thePlayer.Stabilize()
|
|
|
|
}
|
2017-05-16 03:35:58 +02:00
|
|
|
if ebiten.IsRunningSlowly() {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-04 15:00:19 +01:00
|
|
|
screen.Fill(skyColor)
|
|
|
|
updateGroundImage(groundImage)
|
|
|
|
drawGroundImage(screen, groundImage)
|
2016-08-01 18:49:05 +02:00
|
|
|
tutrial := "Space: Move forward\nLeft/Right: Rotate"
|
2016-07-16 22:36:06 +02:00
|
|
|
msg := fmt.Sprintf("FPS: %0.2f\n%s", ebiten.CurrentFPS(), tutrial)
|
2017-03-04 15:00:19 +01:00
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
2016-07-16 22:36:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var err error
|
2016-07-27 18:10:40 +02:00
|
|
|
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest)
|
2016-07-16 22:36:06 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-07-18 10:44:25 +02:00
|
|
|
w, h := gophersImage.Size()
|
|
|
|
const repeat = 5
|
2017-05-16 03:35:58 +02:00
|
|
|
repeatedGophersImage, _ = ebiten.NewImage(w*repeat, h*repeat, ebiten.FilterNearest)
|
2016-07-18 10:44:25 +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))
|
2017-05-16 03:35:58 +02:00
|
|
|
repeatedGophersImage.DrawImage(gophersImage, op)
|
2016-07-18 10:44:25 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-17 22:19:22 +02:00
|
|
|
groundWidth := screenWidth + 70
|
2017-05-16 03:35:58 +02:00
|
|
|
groundImage, _ = ebiten.NewImage(groundWidth, screenHeight*2/3+50, ebiten.FilterNearest)
|
2016-07-17 22:19:22 +02:00
|
|
|
const fogHeight = 8
|
|
|
|
fogRGBA := image.NewRGBA(image.Rect(0, 0, groundWidth, fogHeight))
|
|
|
|
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)
|
|
|
|
for i := 0; i < groundWidth; i++ {
|
|
|
|
fogRGBA.SetRGBA(i, j, clr)
|
|
|
|
}
|
|
|
|
}
|
2017-05-16 03:35:58 +02:00
|
|
|
fogImage, _ = ebiten.NewImageFromImage(fogRGBA, ebiten.FilterNearest)
|
2016-07-16 22:36:06 +02:00
|
|
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Air Ship (Ebiten Demo)"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|