This commit is contained in:
Luke Meyers 2020-02-07 23:51:00 -08:00
parent b97d6c8d5d
commit f7a2b25a63
3 changed files with 70 additions and 23 deletions

View File

@ -1,7 +1,11 @@
package gfx package gfx
import ( import (
"fmt"
"image"
"image/color" "image/color"
_ "image/png"
"os"
"relay/game" "relay/game"
"github.com/faiface/pixel" "github.com/faiface/pixel"
@ -23,8 +27,35 @@ type context struct {
w *pixelgl.Window w *pixelgl.Window
} }
func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderState { type spriteBank struct {
w.Clear(colornames.Olivedrab) bot pixel.Picture
}
func NewSpriteBank() (*spriteBank, error) {
pic, err := loadPicture("simplebot-white.png")
if err != nil {
return nil, fmt.Errorf("load picture: %w", err)
}
return &spriteBank{
bot: pic,
}, nil
}
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window, sb spriteBank) RenderState {
w.Clear(colornames.Black)
colors := teamColors(sNew.Teams) colors := teamColors(sNew.Teams)
ctx := context{ ctx := context{
@ -33,7 +64,7 @@ func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderStat
tween: float64(rs.Frame) / float64(rs.Frames), tween: float64(rs.Frame) / float64(rs.Frames),
w: w, w: w,
} }
renderBots(ctx, colors) renderBots(ctx, colors, sb.bot)
renderObstacles(sNew, w) renderObstacles(sNew, w)
rs.Frame++ rs.Frame++
@ -43,12 +74,12 @@ func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window) RenderStat
return rs return rs
} }
func renderBots(ctx context, colors map[*game.Team]pixel.RGBA) { func renderBots(ctx context, colors map[*game.Team]pixel.RGBA, pic pixel.Picture) {
for i, t := range ctx.sNew.Teams { for i, t := range ctx.sNew.Teams {
c := colors[&ctx.sNew.Teams[i]] c := colors[&ctx.sNew.Teams[i]]
for j, bot := range t.Bots { for j, bot := range t.Bots {
oldBot := ctx.sOld.Teams[i].Bots[j] oldBot := ctx.sOld.Teams[i].Bots[j]
renderBot(oldBot, bot, ctx.sOld, ctx.sNew, ctx.w, c, ctx.tween) renderBot(ctx, oldBot, bot, c, pic)
} }
oldHolder, newHolder := game.ActiveBot(ctx.sOld.Teams[i]), game.ActiveBot(ctx.sNew.Teams[i]) oldHolder, newHolder := game.ActiveBot(ctx.sOld.Teams[i]), game.ActiveBot(ctx.sNew.Teams[i])
@ -63,27 +94,33 @@ func renderBots(ctx context, colors map[*game.Team]pixel.RGBA) {
} }
} }
func renderBot(oldBot, bot game.Bot, sOld, sNew game.State, w *pixelgl.Window, c pixel.RGBA, tween float64) { func renderBot(ctx context, oldBot, bot game.Bot, c pixel.RGBA, pic pixel.Picture) {
im := imdraw.New(nil) im := imdraw.New(nil)
im.Color = c im.Color = c
oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, w.Bounds()) oldPos := lanePos(oldBot.Position.Pos, oldBot.Position.Lane, botWidth, ctx.w.Bounds())
newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, w.Bounds()) newPos := lanePos(bot.Position.Pos, bot.Position.Lane, botWidth, ctx.w.Bounds())
pos := pixel.Vec{ pos := pixel.Vec{
X: oldPos.X + tween*(newPos.X-oldPos.X), X: oldPos.X + ctx.tween*(newPos.X-oldPos.X),
Y: oldPos.Y + tween*(newPos.Y-oldPos.Y), Y: oldPos.Y + ctx.tween*(newPos.Y-oldPos.Y),
} }
im.Push(pos) im.Push(pos)
im.Clear() im.Clear()
im.Circle(botWidth, 0) //im.Circle(botWidth, 0)
im.Draw(w) im.Draw(ctx.w)
bounds := pic.Bounds()
//log.Println("bounds:", bounds)
//bounds = bounds.Resized(bounds.Center(), pixel.Vec{bounds.W() * 2, bounds.H() * 4})
//log.Println("resize:", bounds)
sprite := pixel.NewSprite(pic, bounds)
sprite.DrawColorMask(ctx.w, pixel.IM.Moved(pos).ScaledXY(pos, pixel.Vec{3, 3}), c)
} }
func renderBaton(pos pixel.Vec, w *pixelgl.Window) { func renderBaton(pos pixel.Vec, w *pixelgl.Window) {
im := imdraw.New(nil) im := imdraw.New(nil)
im.Color = pixel.RGB(0, 0, 0) im.Color = colornames.Bisque
im.Push(pos) im.Push(pos)
im.Clear() im.Clear()
im.Circle(batonWidth, 3) im.Circle(batonWidth, 3)
@ -103,7 +140,7 @@ func renderObstacles(s game.State, w *pixelgl.Window) {
im := imdraw.New(nil) im := imdraw.New(nil)
for _, o := range s.Obstacles { for _, o := range s.Obstacles {
im.Color = pixel.RGB(0.1, 0.1, 0.2) im.Color = colornames.Slategray
pos := lanePos(o.Position.Pos, o.Position.Lane, botWidth, b) pos := lanePos(o.Position.Pos, o.Position.Lane, botWidth, b)
@ -126,13 +163,15 @@ func teamColors(ts []game.Team) map[*game.Team]pixel.RGBA {
case 1: case 1:
c = colornames.Green c = colornames.Green
case 2: case 2:
c = colornames.Blue c = colornames.Cornflowerblue
case 3: case 3:
c = colornames.Magenta c = colornames.Magenta
case 4: case 4:
c = colornames.Cyan c = colornames.Cyan
case 5: case 5:
c = colornames.Yellow c = colornames.Yellow
case 6:
c = colornames.Blueviolet
} }
m[&ts[i]] = pixel.ToRGBA(c) m[&ts[i]] = pixel.ToRGBA(c)
} }

24
main.go
View File

@ -9,10 +9,9 @@ import (
"github.com/faiface/pixel" "github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl" "github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
) )
func run() { func run() error {
cfg := pixelgl.WindowConfig{ cfg := pixelgl.WindowConfig{
Title: "Relay", Title: "Relay",
Bounds: pixel.R(0, 0, 2048, 512), Bounds: pixel.R(0, 0, 2048, 512),
@ -21,28 +20,30 @@ func run() {
w, err := pixelgl.NewWindow(cfg) w, err := pixelgl.NewWindow(cfg)
if err != nil { if err != nil {
panic(err) return err
} }
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
s := game.NewState() s := game.NewState()
w.Clear(colornames.Peachpuff)
rs := gfx.RenderState{ rs := gfx.RenderState{
Animating: true, Animating: true,
Frames: 20, Frames: 20,
} }
sb, err := gfx.NewSpriteBank()
if err != nil {
return err
}
sOld := s sOld := s
turn := 1 turn := 1
for !w.Closed() && !s.GameOver { for !w.Closed() && !s.GameOver {
switch { switch {
case w.Pressed(pixelgl.KeyQ): case w.Pressed(pixelgl.KeyQ):
return return nil
case rs.Animating: case rs.Animating:
rs = gfx.Render(rs, sOld, s, w) rs = gfx.Render(rs, sOld, s, w, *sb)
if !rs.Animating { if !rs.Animating {
sOld = s sOld = s
} }
@ -61,8 +62,15 @@ func run() {
w.Update() w.Update()
} }
return nil
}
func pixelRun() {
if err := run(); err != nil {
log.Fatal(err)
}
} }
func main() { func main() {
pixelgl.Run(run) pixelgl.Run(pixelRun)
} }

BIN
simplebot-white.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 B