mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-13 22:47:26 +01:00
Add example/platform (WIP)
This commit is contained in:
parent
c3eb4e98c3
commit
b2c3a5bb73
BIN
example/images/platform/tileset.png
Normal file
BIN
example/images/platform/tileset.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
40
example/platform/main.go
Normal file
40
example/platform/main.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// Copyright 2015 Hajime Hoshi
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/example/platform/platform"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ScreenWidth = 256
|
||||||
|
ScreenHeight = 240
|
||||||
|
ScreenScale = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
var game = platform.NewGame()
|
||||||
|
|
||||||
|
func update(screen *ebiten.Image) error {
|
||||||
|
game.Update()
|
||||||
|
return game.Draw(screen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := ebiten.Run(update, ScreenWidth, ScreenHeight, ScreenScale, "Platform (Ebiten Demo)"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
114
example/platform/platform/game.go
Normal file
114
example/platform/platform/game.go
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
// Copyright 2015 Hajime Hoshi
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package platform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NOTE: It's painful to prepare a struct to draw an image part.
|
||||||
|
type playerRect struct {
|
||||||
|
game *Game
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *playerRect) Len() int {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *playerRect) Src(i int) (x0, y0, x1, y1 int) {
|
||||||
|
return 0, 128, TileSize, 128 + TileSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *playerRect) Dst(i int) (x0, y0, x1, y1 int) {
|
||||||
|
x, y := (p.game.player.x)/unit-8, (p.game.player.y)/unit-16
|
||||||
|
return x, y, x + TileSize, y + TileSize
|
||||||
|
}
|
||||||
|
|
||||||
|
type player struct {
|
||||||
|
x int
|
||||||
|
y int
|
||||||
|
vy int
|
||||||
|
ay int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Game struct {
|
||||||
|
level *Level
|
||||||
|
player *player
|
||||||
|
jumpKeyState int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGame() *Game {
|
||||||
|
x, y := (TileSize*3+8)*unit, (TileSize*13)*unit
|
||||||
|
return &Game{
|
||||||
|
level: &Level{},
|
||||||
|
player: &player{
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
vy: 0,
|
||||||
|
ay: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Rename
|
||||||
|
const unit = 16
|
||||||
|
|
||||||
|
func (g *Game) inAir() bool {
|
||||||
|
return g.player.y < 13*unit*TileSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Update() {
|
||||||
|
if ebiten.IsKeyPressed(ebiten.KeySpace) {
|
||||||
|
g.jumpKeyState++
|
||||||
|
} else {
|
||||||
|
g.jumpKeyState = 0
|
||||||
|
}
|
||||||
|
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
|
||||||
|
g.player.x -= 2 * unit
|
||||||
|
} else if ebiten.IsKeyPressed(ebiten.KeyRight) {
|
||||||
|
g.player.x += 2 * unit
|
||||||
|
}
|
||||||
|
if g.inAir() {
|
||||||
|
g.player.ay = unit
|
||||||
|
if g.player.vy < 0 && 0 < g.jumpKeyState {
|
||||||
|
g.player.ay -= int(0.5 * unit)
|
||||||
|
}
|
||||||
|
} else if g.jumpKeyState == 1 {
|
||||||
|
g.player.ay = -10 * unit
|
||||||
|
} else {
|
||||||
|
g.player.vy = 0
|
||||||
|
g.player.ay = 0
|
||||||
|
}
|
||||||
|
if 5*unit < g.player.vy {
|
||||||
|
g.player.vy = 5 * unit
|
||||||
|
}
|
||||||
|
g.player.vy += g.player.ay
|
||||||
|
g.player.y += g.player.vy
|
||||||
|
if 13*unit*TileSize <= g.player.y {
|
||||||
|
g.player.y = 13 * unit * TileSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Draw(screen *ebiten.Image) error {
|
||||||
|
if err := g.level.Draw(screen); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := screen.DrawImage(tileSet, &ebiten.DrawImageOptions{
|
||||||
|
ImageParts: &playerRect{g},
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
78
example/platform/platform/level.go
Normal file
78
example/platform/platform/level.go
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
// Copyright 2015 Hajime Hoshi
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package platform
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ScreenWidth = 256
|
||||||
|
ScreenHeight = 240
|
||||||
|
)
|
||||||
|
|
||||||
|
var tileSet *ebiten.Image
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
tileSet, _, err = ebitenutil.NewImageFromFile("images/platform/tileset.png", ebiten.FilterNearest)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
TileSize = 16
|
||||||
|
TileSrcXNum = 8
|
||||||
|
TileDstXNum = ScreenWidth / TileSize
|
||||||
|
TileDstYNum = ScreenHeight / TileSize
|
||||||
|
)
|
||||||
|
|
||||||
|
type levelRects struct {
|
||||||
|
*Level
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *levelRects) Len() int {
|
||||||
|
return TileDstXNum * TileDstYNum
|
||||||
|
}
|
||||||
|
|
||||||
|
func tileRect(i int) (x0, y0, x1, y1 int) {
|
||||||
|
x := (i % TileSrcXNum) * TileSize
|
||||||
|
y := (i / TileSrcXNum) * TileSize
|
||||||
|
return x, y, x + TileSize, y + TileSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *levelRects) Src(i int) (x0, y0, x1, y1 int) {
|
||||||
|
if (i / TileDstXNum) < 13 {
|
||||||
|
return tileRect(1)
|
||||||
|
}
|
||||||
|
return tileRect(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *levelRects) Dst(i int) (x0, y0, x1, y1 int) {
|
||||||
|
x := (i % TileDstXNum) * TileSize
|
||||||
|
y := (i / TileDstXNum) * TileSize
|
||||||
|
return x, y, x + TileSize, y + TileSize
|
||||||
|
}
|
||||||
|
|
||||||
|
type Level struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Level) Draw(screen *ebiten.Image) error {
|
||||||
|
return screen.DrawImage(tileSet, &ebiten.DrawImageOptions{
|
||||||
|
ImageParts: &levelRects{l},
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user