boxbrawl/component/player.go

127 lines
2.0 KiB
Go

package component
import (
"fmt"
"image"
"image/color"
)
type PlayerAction int
const (
ActionIdle PlayerAction = iota
ActionStunned
ActionBlock
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),
},
},
}, { // ActionStunned
{
// No hitboxes or hurtboxes while stunned.
},
}, { // ActionBlock
{
// TODO
},
}, { // ActionPunch
{
{
T: HitboxNormal,
R: image.Rect(0, 0, PlayerSize, PlayerSize),
},
{
T: HitboxHurt,
R: image.Rect(-5, -5, PlayerSize+10, PlayerSize+10),
},
},
{
{
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),
},
},
},
}
func FrameDataForActionTick(a PlayerAction, tick int) []FrameData {
actionFrames := AllPlayerFrames[a]
if tick-1 >= len(actionFrames) {
return nil
}
return actionFrames[tick-1]
}
type Player struct {
X, Y float64
VX, VY 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 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
}