78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package player
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.wisellama.rocks/Project-Ely/project-ely/internal/channels"
|
|
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/command"
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
|
)
|
|
|
|
// A player represents a collection of stuff controlled by the user's input.
|
|
// It contains the camera used to view the world,
|
|
// the viewport for the part of the screen to draw to (splitscreen support),
|
|
// as well as the entity the player is currently controlling.
|
|
type player struct {
|
|
ctx context.Context
|
|
timeout time.Duration
|
|
|
|
entityChan chan command.Command
|
|
}
|
|
|
|
func NewPlayer(ctx context.Context) *player {
|
|
defaultTimeout := time.Second
|
|
|
|
p := player{
|
|
ctx: ctx,
|
|
timeout: defaultTimeout,
|
|
}
|
|
return &p
|
|
}
|
|
|
|
func (p *player) SetEntityChan(e chan command.Command) {
|
|
p.entityChan = e
|
|
}
|
|
|
|
func (p *player) Update() {
|
|
var (
|
|
up bool
|
|
down bool
|
|
left bool
|
|
right bool
|
|
speed bool
|
|
)
|
|
channels.RL.Do(func() {
|
|
speed = rl.IsKeyDown(rl.KeyLeftShift)
|
|
left = rl.IsKeyDown(rl.KeyA) || rl.IsKeyDown(rl.KeyLeft)
|
|
right = rl.IsKeyDown(rl.KeyD) || rl.IsKeyDown(rl.KeyRight)
|
|
up = rl.IsKeyDown(rl.KeyW) || rl.IsKeyDown(rl.KeyUp)
|
|
down = rl.IsKeyDown(rl.KeyS) || rl.IsKeyDown(rl.KeyDown)
|
|
})
|
|
|
|
// Speed
|
|
if speed {
|
|
p.entityChan <- command.NewCommand(command.SET_SPEED, 4)
|
|
} else {
|
|
p.entityChan <- command.NewCommand(command.SET_SPEED, 2)
|
|
}
|
|
|
|
// Move horizontal
|
|
if right {
|
|
p.entityChan <- command.NewCommand(command.MOVE_X, 1.0)
|
|
} else if left {
|
|
p.entityChan <- command.NewCommand(command.MOVE_X, -1.0)
|
|
} else if !right && !left {
|
|
p.entityChan <- command.NewCommand(command.MOVE_X, 0.0)
|
|
}
|
|
|
|
// Move vertical
|
|
if up {
|
|
p.entityChan <- command.NewCommand(command.MOVE_Y, 1.0)
|
|
} else if down {
|
|
p.entityChan <- command.NewCommand(command.MOVE_Y, -1.0)
|
|
} else if !up && !down {
|
|
p.entityChan <- command.NewCommand(command.MOVE_Y, 0.0)
|
|
}
|
|
}
|