boxbrawl/component/player.go

109 lines
1.7 KiB
Go

package component
import (
"fmt"
"image"
"image/color"
)
type PlayerAction int
const (
ActionIdle PlayerAction = iota
ActionPunch
)
type HitboxType int
const (
HitboxInvalid HitboxType = iota
HitboxNormal
HitboxHurt
)
type FrameData struct {
T HitboxType
R image.Rectangle
}
const playerSize = 20
// AllPlayerFrames defines all frame data for the game. Frames are defined in reverse order.
var AllPlayerFrames = [][][]FrameData{
{ // ActionIdle
{
{
T: HitboxNormal,
R: image.Rect(0, 0, playerSize, playerSize),
},
},
}, { // ActionPunch
{
{
T: HitboxNormal,
R: image.Rect(0, 0, playerSize, playerSize),
},
{
T: HitboxHurt,
R: image.Rect(0, 0, playerSize, playerSize),
},
},
{
{
T: HitboxNormal,
R: image.Rect(0, 0, playerSize, playerSize),
},
},
{
{
T: HitboxNormal,
R: image.Rect(0, 0, playerSize, playerSize),
},
},
{
{
T: HitboxNormal,
R: image.Rect(0, 0, playerSize, playerSize),
},
},
{
{
T: HitboxNormal,
R: image.Rect(0, 0, playerSize, playerSize),
},
},
{
{
T: HitboxNormal,
R: image.Rect(0, 0, playerSize, playerSize),
},
},
},
}
type Player struct {
X float64
Y float64
Color color.Color
PlayerNum int
Action PlayerAction
ActionTicksLeft int
}
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 OffsetRect(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
}