organização de código

This commit is contained in:
silva guimaraes 2025-01-28 08:23:02 -03:00
parent 277c6d070a
commit a411573a06
5 changed files with 529 additions and 266 deletions

117
player.go Normal file
View file

@ -0,0 +1,117 @@
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
func (p *player) move(g *game) {
var moveSpeed float32
if p.focusMode {
moveSpeed = p.moveSpeed * p.focusSpeedDecrease
} else {
moveSpeed = p.moveSpeed
}
p.speed = rl.Vector2{X: 0, Y: 0}
if rl.IsKeyDown(rl.KeyW) {
p.speed.Y -= 1
}
if rl.IsKeyDown(rl.KeyS) {
p.speed.Y += 1
}
if rl.IsKeyDown(rl.KeyA) {
p.speed.X -= 1
}
if rl.IsKeyDown(rl.KeyD) {
p.speed.X += 1
}
if !(p.speed.X == 0 && p.speed.Y == 0) {
// jogador se move mais rapido na diagonal caso o contrario
p.speed = rl.Vector2Normalize(p.speed)
p.speed = rl.Vector2Scale(p.speed, moveSpeed)
}
result := rl.Vector2Add(p.pos, p.speed)
if result.Y-p.hitBoxRadius < 0 || result.Y+p.hitBoxRadius > float32(g.arenaHeight) {
p.speed.Y = 0
}
if result.X-p.hitBoxRadius < 0 || result.X+p.hitBoxRadius > float32(g.arenaWidth) {
p.speed.X = 0
}
p.pos = rl.Vector2Add(p.pos, p.speed)
}
func (p *player) shoot(g *game) {
if p.focusMode {
return
}
if int(g.frame)%3 != 0 {
return
}
if rl.IsMouseButtonDown(rl.MouseLeftButton) {
mouse := rl.GetMousePosition()
direction := rl.Vector2Subtract(mouse, p.pos)
direction = rl.Vector2Add(direction, p.speed)
direction = rl.Vector2Normalize(direction)
direction = rl.Vector2Scale(direction, p.bulletMoveSpeed)
g.bullets = append(g.bullets, &bullet{
pos: p.pos,
size: p.bulletSize,
speed: direction,
dmg: 1,
enemy: false,
})
}
}
func (p *player) checkHit(g *game) {
for _, bullet := range g.bullets {
if !bullet.enemy {
continue
}
distance := rl.Vector2Distance(p.pos, bullet.pos) - bullet.size
if distance < p.hitBoxRadius {
// fmt.Println("hit!")
}
}
}
func (p *player) checkFocus() {
// raylib não entende keybindings customizadas através do xmodmap?
p.focusMode = rl.IsKeyDown(rl.KeyLeftShift) || rl.IsKeyDown(rl.KeyRightShift)
}
func (p *player) update(g *game) {
p.checkFocus()
p.move(g)
p.shoot(g)
p.checkHit(g)
// hitbox
rl.DrawCircleV(p.pos, p.hitBoxRadius, rl.Red)
if p.focusMode {
return
}
// mira
mouse := rl.GetMousePosition()
inverted := rl.Vector2Subtract(p.pos, mouse)
inverted = rl.Vector2Negate(inverted)
inverted = rl.Vector2Normalize(inverted)
inverted = rl.Vector2Scale(inverted, 2000)
rl.DrawLineV(p.pos, rl.Vector2Add(mouse, inverted), rl.NewColor(255, 0, 0, 100))
}