Added proper taggins for gravity
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/atridad/LilGuy/internal/config"
|
||||
"github.com/atridad/LilGuy/internal/projectile"
|
||||
"github.com/atridad/LilGuy/internal/world"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -49,6 +50,14 @@ const (
|
||||
DirRight
|
||||
)
|
||||
|
||||
// Tags
|
||||
|
||||
type Tag int
|
||||
|
||||
const (
|
||||
TagGravity Tag = iota
|
||||
)
|
||||
|
||||
// Hero state
|
||||
|
||||
type Hero struct {
|
||||
@@ -63,6 +72,8 @@ type Hero struct {
|
||||
|
||||
Color color.NRGBA
|
||||
|
||||
Tags map[Tag]bool
|
||||
|
||||
Stamina float64
|
||||
MaxStamina float64
|
||||
StaminaDrain float64
|
||||
@@ -118,6 +129,7 @@ func New(cfg Config) *Hero {
|
||||
Radius: cfg.Radius,
|
||||
Speed: cfg.Speed,
|
||||
Color: cfg.Color,
|
||||
Tags: map[Tag]bool{TagGravity: true},
|
||||
Stamina: cfg.MaxStamina,
|
||||
MaxStamina: cfg.MaxStamina,
|
||||
StaminaDrain: cfg.StaminaDrain,
|
||||
@@ -136,42 +148,69 @@ func New(cfg Config) *Hero {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hero) Update(input Input, dt float64, bounds Bounds) {
|
||||
func (h *Hero) Update(input Input, dt float64, bounds Bounds, w *world.World) {
|
||||
if dt > 0.1 {
|
||||
dt = 0.1
|
||||
}
|
||||
h.updateMovement(input, dt, bounds)
|
||||
h.updateMovement(input, dt, bounds, w)
|
||||
h.updateStamina(input, dt)
|
||||
h.updateAnimation(dt)
|
||||
}
|
||||
|
||||
// Movement and physics
|
||||
|
||||
func (h *Hero) updateMovement(input Input, dt float64, bounds Bounds) {
|
||||
// apply gravity
|
||||
h.VelocityY += config.Gravity * dt
|
||||
func (h *Hero) updateMovement(input Input, dt float64, bounds Bounds, w *world.World) {
|
||||
if h.HasTag(TagGravity) {
|
||||
h.VelocityY += config.Gravity * dt
|
||||
}
|
||||
if h.VelocityY > config.MaxFallSpeed {
|
||||
h.VelocityY = config.MaxFallSpeed
|
||||
}
|
||||
|
||||
newY := h.Y + h.VelocityY*dt
|
||||
|
||||
wasGrounded := h.isGrounded
|
||||
if newY >= bounds.Ground {
|
||||
h.Y = bounds.Ground
|
||||
h.VelocityY = 0
|
||||
h.isGrounded = true
|
||||
} else {
|
||||
h.Y = newY
|
||||
h.isGrounded = false
|
||||
h.isGrounded = false
|
||||
|
||||
if w != nil && h.VelocityY >= 0 {
|
||||
feetX := h.X
|
||||
var landedSurface *world.Surface
|
||||
|
||||
for _, s := range w.Surfaces {
|
||||
if !s.IsWalkable() {
|
||||
continue
|
||||
}
|
||||
|
||||
if feetX >= s.X && feetX <= s.X+s.Width {
|
||||
if h.Y <= s.Y && newY >= s.Y {
|
||||
if landedSurface == nil || s.Y < landedSurface.Y {
|
||||
landedSurface = s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if landedSurface != nil {
|
||||
newY = landedSurface.Y
|
||||
h.VelocityY = 0
|
||||
h.isGrounded = true
|
||||
}
|
||||
}
|
||||
|
||||
if input.Jump && (h.isGrounded || (!wasGrounded && h.isGrounded)) {
|
||||
if !h.isGrounded {
|
||||
if newY >= bounds.Ground {
|
||||
newY = bounds.Ground
|
||||
h.VelocityY = 0
|
||||
h.isGrounded = true
|
||||
}
|
||||
}
|
||||
|
||||
h.Y = newY
|
||||
|
||||
if input.Jump && h.isGrounded {
|
||||
h.VelocityY = config.JumpStrength
|
||||
h.isGrounded = false
|
||||
}
|
||||
|
||||
// horizontal input
|
||||
targetVelocityX := 0.0
|
||||
if input.Left {
|
||||
targetVelocityX -= h.Speed
|
||||
@@ -184,7 +223,6 @@ func (h *Hero) updateMovement(input Input, dt float64, bounds Bounds) {
|
||||
|
||||
h.isMoving = targetVelocityX != 0
|
||||
|
||||
// sprinting
|
||||
h.isSprinting = input.Sprint && h.canSprint && h.Stamina > 0 && h.isMoving
|
||||
if h.isSprinting {
|
||||
targetVelocityX *= config.SprintSpeedMultiplier
|
||||
@@ -329,6 +367,27 @@ func (h *Hero) getCurrentSprite() *ebiten.Image {
|
||||
return getKnightSprite(h.direction, h.isMoving, h.animFrame)
|
||||
}
|
||||
|
||||
func (h *Hero) AddTag(tag Tag) {
|
||||
if h.Tags == nil {
|
||||
h.Tags = make(map[Tag]bool)
|
||||
}
|
||||
h.Tags[tag] = true
|
||||
}
|
||||
|
||||
func (h *Hero) RemoveTag(tag Tag) {
|
||||
if h.Tags == nil {
|
||||
return
|
||||
}
|
||||
delete(h.Tags, tag)
|
||||
}
|
||||
|
||||
func (h *Hero) HasTag(tag Tag) bool {
|
||||
if h.Tags == nil {
|
||||
return false
|
||||
}
|
||||
return h.Tags[tag]
|
||||
}
|
||||
|
||||
func (h *Hero) GetDirection() Direction {
|
||||
return h.direction
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user