mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-23 17:32:02 +01:00
examples/airship: Refactoring
This commit is contained in:
parent
444e5bab84
commit
6abaf41440
@ -31,7 +31,6 @@ import (
|
|||||||
const (
|
const (
|
||||||
screenWidth = 320
|
screenWidth = 320
|
||||||
screenHeight = 240
|
screenHeight = 240
|
||||||
groundWidth = screenWidth + 70
|
|
||||||
maxAngle = 256
|
maxAngle = 256
|
||||||
maxLean = 16
|
maxLean = 16
|
||||||
)
|
)
|
||||||
@ -43,10 +42,11 @@ var (
|
|||||||
y16: 16 * 200,
|
y16: 16 * 200,
|
||||||
angle: maxAngle * 3 / 4,
|
angle: maxAngle * 3 / 4,
|
||||||
}
|
}
|
||||||
gophersImage *ebiten.Image
|
gophersImage *ebiten.Image
|
||||||
repeatedGophersImage *ebiten.Image
|
repeatedGophersImage *ebiten.Image
|
||||||
groundImage *ebiten.Image
|
groundImage *ebiten.Image
|
||||||
fogImage *ebiten.Image
|
perspectiveGroundImage *ebiten.Image
|
||||||
|
fogImage *ebiten.Image
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -55,7 +55,8 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
groundImage, _ = ebiten.NewImage(groundWidth, screenHeight*2/3+50, ebiten.FilterNearest)
|
groundImage, _ = ebiten.NewImage(screenWidth*2, screenHeight*2/3+50, ebiten.FilterNearest)
|
||||||
|
perspectiveGroundImage, _ = ebiten.NewImage(screenWidth*2, screenHeight, ebiten.FilterNearest)
|
||||||
|
|
||||||
const repeat = 5
|
const repeat = 5
|
||||||
w, h := gophersImage.Size()
|
w, h := gophersImage.Size()
|
||||||
@ -67,11 +68,10 @@ func init() {
|
|||||||
repeatedGophersImage.DrawImage(gophersImage, op)
|
repeatedGophersImage.DrawImage(gophersImage, op)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
const fogHeight = 8
|
const fogHeight = 8
|
||||||
fogRGBA := image.NewRGBA(image.Rect(0, 0, groundWidth, fogHeight))
|
w, _ = perspectiveGroundImage.Size()
|
||||||
|
fogRGBA := image.NewRGBA(image.Rect(0, 0, w, fogHeight))
|
||||||
for j := 0; j < fogHeight; j++ {
|
for j := 0; j < fogHeight; j++ {
|
||||||
a := uint32(float64(fogHeight-1-j) * 0xff / (fogHeight - 1))
|
a := uint32(float64(fogHeight-1-j) * 0xff / (fogHeight - 1))
|
||||||
clr := skyColor
|
clr := skyColor
|
||||||
@ -80,7 +80,7 @@ func init() {
|
|||||||
clr.G = uint8(g * a / oa)
|
clr.G = uint8(g * a / oa)
|
||||||
clr.B = uint8(b * a / oa)
|
clr.B = uint8(b * a / oa)
|
||||||
clr.A = uint8(a)
|
clr.A = uint8(a)
|
||||||
for i := 0; i < groundWidth; i++ {
|
for i := 0; i < w; i++ {
|
||||||
fogRGBA.SetRGBA(i, j, clr)
|
fogRGBA.SetRGBA(i, j, clr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -185,6 +185,7 @@ func scaleForLine(y int) float64 {
|
|||||||
//
|
//
|
||||||
// c: camera
|
// c: camera
|
||||||
// s: intersection of the ray and the screen
|
// s: intersection of the ray and the screen
|
||||||
|
// A-c: camera height
|
||||||
// B-s: horizon height
|
// B-s: horizon height
|
||||||
// A-C: far point on the plane
|
// A-C: far point on the plane
|
||||||
// A-B: the distance from the camera to the screen
|
// A-B: the distance from the camera to the screen
|
||||||
@ -192,7 +193,7 @@ func scaleForLine(y int) float64 {
|
|||||||
// The ground image is on the plane, and the head of the ground image is on 'C'.
|
// The ground image is on the plane, and the head of the ground image is on 'C'.
|
||||||
const (
|
const (
|
||||||
horizonHeight = screenHeight * 2.0 / 3.0
|
horizonHeight = screenHeight * 2.0 / 3.0
|
||||||
cameraHeight = horizonHeight + 200.0
|
cameraHeight = horizonHeight + 100.0
|
||||||
farPointOnPlane = 200.0
|
farPointOnPlane = 200.0
|
||||||
cameraToScreen = farPointOnPlane - farPointOnPlane/cameraHeight*horizonHeight
|
cameraToScreen = farPointOnPlane - farPointOnPlane/cameraHeight*horizonHeight
|
||||||
)
|
)
|
||||||
@ -205,31 +206,34 @@ func scaleForLine(y int) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
|
func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
|
||||||
w, h := ground.Size()
|
perspectiveGroundImage.Clear()
|
||||||
g := ebiten.GeoM{}
|
gw, gh := ground.Size()
|
||||||
g.Translate(-float64(w)/2, 0)
|
pw, ph := perspectiveGroundImage.Size()
|
||||||
g.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8)
|
|
||||||
g.Translate(float64(w)/2, 0)
|
|
||||||
g.Translate(float64(screenWidth-w)/2, screenHeight/3)
|
|
||||||
op := &ebiten.DrawImageOptions{}
|
|
||||||
y := 0.0
|
y := 0.0
|
||||||
for i := 0; i < h; i++ {
|
for i := 0; i < ph; i++ {
|
||||||
op.GeoM.Reset()
|
op := &ebiten.DrawImageOptions{}
|
||||||
s := scaleForLine(i)
|
s := scaleForLine(i)
|
||||||
dx0, dy0 := float64(w)*(1-s)/2, y
|
dx0 := -float64(gw)*s/2 + float64(pw)/2
|
||||||
op.GeoM.Scale(s, s+1)
|
op.GeoM.Scale(s, s+1)
|
||||||
op.GeoM.Translate(float64(dx0), float64(dy0))
|
op.GeoM.Translate(float64(dx0), y)
|
||||||
op.GeoM.Concat(g)
|
|
||||||
|
|
||||||
src := image.Rect(0, i, w, i+1)
|
src := image.Rect(0, i, gw, i+1)
|
||||||
op.SourceRect = &src
|
op.SourceRect = &src
|
||||||
screen.DrawImage(ground, op)
|
perspectiveGroundImage.DrawImage(ground, op)
|
||||||
|
|
||||||
y += s
|
y += s
|
||||||
|
if y > float64(gh) {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
op = &ebiten.DrawImageOptions{}
|
|
||||||
op.GeoM = g
|
perspectiveGroundImage.DrawImage(fogImage, nil)
|
||||||
screen.DrawImage(fogImage, op)
|
|
||||||
|
op := &ebiten.DrawImageOptions{}
|
||||||
|
op.GeoM.Translate(-float64(pw)/2, 0)
|
||||||
|
op.GeoM.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8)
|
||||||
|
op.GeoM.Translate(float64(screenWidth)/2, screenHeight/3)
|
||||||
|
screen.DrawImage(perspectiveGroundImage, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
func update(screen *ebiten.Image) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user