boxbrawl/component/player.go

125 lines
2.4 KiB
Go

package component
import (
"fmt"
"image"
"image/color"
"code.rocketnine.space/tslocum/boxbrawl/asset"
"github.com/hajimehoshi/ebiten/v2"
)
type PlayerAction int
const (
ActionIdle PlayerAction = iota
ActionStunned
ActionPunch
ActionKick
ActionBlock
ActionTaunt1
ActionTaunt2
ActionTaunt3
ActionTaunt4
)
type HitboxType int
const (
HitboxNone HitboxType = iota
HitboxNormal
HitboxHurt
)
type FrameData struct {
T HitboxType // Type
R image.Rectangle // Rect
S *ebiten.Image // Sprite
G int // Go to frame (for looping actions)
}
const PlayerSize = 16
const (
PlayerHeight = 48
PlayerWidth = 16
)
func RectTouches(r1 image.Rectangle, r2 image.Rectangle) bool {
return !r1.Empty() && !r2.Empty() &&
r1.Min.X <= r2.Max.X && r2.Min.X <= r1.Max.X &&
r1.Min.Y <= r2.Max.Y && r2.Min.Y <= r1.Max.Y
}
// stdHit returns FrameData using a standard hitbox and the provided sprite.
func stdHit(sprite *ebiten.Image) FrameData {
return FrameData{
T: HitboxNormal,
R: image.Rect(0, 0, PlayerWidth, PlayerHeight),
S: sprite,
}
}
// stdHitRange returns FrameData using a standard hitbox and the provided sprite range.
func stdHitRange(sprite *ebiten.Image, spriteX int, spriteY int, size int) []FrameData {
data := make([]FrameData, size)
for i := 0; i < size; i++ {
data[i] = stdHit(asset.FrameAt(asset.ImgPlayer, spriteX+i, spriteY))
}
return data
}
func FrameDataForActionTick(a PlayerAction, tick int) []FrameData {
actionFrames := AllPlayerFrames[a]
if tick-1 >= len(actionFrames) {
// Handle
if a == ActionStunned {
return AllPlayerFrames[ActionStunned][0]
}
return nil
}
return actionFrames[tick-1]
}
func FlipRect(r image.Rectangle, flip bool) image.Rectangle {
if !flip {
return r
}
return image.Rect(-r.Min.X+PlayerSize, r.Min.Y, -r.Max.X+PlayerSize, r.Max.Y)
}
func PlayerOnRightSide(p Player, o Player) bool {
return p.X > o.X
}
type Player struct {
X, Y float64
VX, VY float64
Color color.Color
PlayerNum int
Action PlayerAction
ActionTicksLeft int
NoPunch bool
PlayAgain bool
}
func (p *Player) String() string {
return fmt.Sprintf("Player %d: X:%f Y:%f Action: %d", p.PlayerNum, p.X, p.Y, p.Action)
}
func (p *Player) Clone() Player {
result := Player{}
result = *p
return result
}
func TranslateRect(r image.Rectangle, x int, y int) image.Rectangle {
r.Min.X, r.Min.Y = r.Min.X+x, r.Min.Y+y
r.Max.X, r.Max.Y = r.Max.X+x, r.Max.Y+y
return r
}