295 lines
6.7 KiB
Go
295 lines
6.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
|
)
|
|
|
|
type movementPattern int
|
|
type shootingPattern int
|
|
|
|
const (
|
|
horizontal movementPattern = iota
|
|
circular movementPattern = iota
|
|
)
|
|
|
|
type game struct {
|
|
screenWidth int32
|
|
screenHeight int32
|
|
time int
|
|
enemies []*enemy
|
|
bullets []*bullet
|
|
}
|
|
|
|
type bullet struct {
|
|
pos rl.Vector2
|
|
speed rl.Vector2
|
|
size float32
|
|
dmg int
|
|
enemy bool
|
|
}
|
|
|
|
type player struct {
|
|
pos rl.Vector2
|
|
moveSpeed float32
|
|
bulletMoveSpeed float32
|
|
hitBoxRadius float32
|
|
}
|
|
|
|
type enemy struct {
|
|
pos rl.Vector2
|
|
health int
|
|
movePattern func(*enemy)rl.Vector2
|
|
shootPattern func()
|
|
hitBoxRadius float32
|
|
}
|
|
|
|
func (g game) insideArena(v rl.Vector2) bool {
|
|
return v.X >= 0 && v.Y >= 0 &&
|
|
v.Y <= float32(g.screenHeight) && v.X <= float32(g.screenWidth)
|
|
}
|
|
|
|
func (g *game) removeBullet(index int) {
|
|
g.bullets[index] = g.bullets[len(g.bullets)-1]
|
|
g.bullets = g.bullets[:len(g.bullets)-1]
|
|
}
|
|
|
|
func (b *bullet) update(g *game, index int) {
|
|
b.pos = rl.Vector2Add(b.pos, b.speed)
|
|
|
|
if !g.insideArena(b.pos) {
|
|
g.removeBullet(index)
|
|
return
|
|
}
|
|
|
|
rl.DrawCircleV(b.pos, b.size, rl.Yellow)
|
|
}
|
|
|
|
func (e *enemy) shoot(g *game) {
|
|
if g.time % 15 != 0 {
|
|
return
|
|
}
|
|
|
|
|
|
g.bullets = append(g.bullets, &bullet{
|
|
speed: rl.Vector2{X: 0, Y: 10},
|
|
size: 4,
|
|
dmg: 1,
|
|
enemy: true,
|
|
pos: e.pos,
|
|
})
|
|
}
|
|
|
|
func horizonalPattern(g *game) (func(*enemy)rl.Vector2) {
|
|
direction := rl.Vector2{X: 4, Y: 0}
|
|
|
|
return func(e *enemy) rl.Vector2 {
|
|
result := rl.Vector2Add(direction, e.pos)
|
|
|
|
if !g.insideArena(result) {
|
|
direction = rl.Vector2Negate(direction)
|
|
}
|
|
return result
|
|
}
|
|
}
|
|
|
|
func circlePattern(g *game, center rl.Vector2) (func(*enemy)rl.Vector2) {
|
|
|
|
t := float64(0.5)
|
|
|
|
return func(e *enemy) rl.Vector2 {
|
|
// x' = (x - x₀) * cos(θ) - (y - y₀) * sin(θ) + x₀
|
|
// y' = (x - x₀) * sin(θ) + (y - y₀) * cos(θ) + y₀
|
|
|
|
// x' = x * cos(θ) - y * sin(θ)
|
|
// y' = x * sin(θ) + y * cos(θ)
|
|
|
|
// result := center
|
|
//
|
|
// result.X = result.X + (e.pos.X - result.X) * float32(math.Cos(t)) -
|
|
// (e.pos.Y - result.Y) * float32(math.Sin(t))
|
|
//
|
|
// result.Y = result.Y + (e.pos.Y - result.Y) * float32(math.Sin(t)) +
|
|
// (e.pos.Y - result.Y) * float32(math.Cos(t))
|
|
|
|
result := center
|
|
result.X = result.X * float32(math.Cos(t)) - result.Y * float32(math.Sin(t))
|
|
result.Y = result.X * float32(math.Sin(t)) - result.Y * float32(math.Cos(t))
|
|
|
|
t += .08
|
|
return result
|
|
}
|
|
}
|
|
|
|
func (e *enemy) checkHit(g *game) (bool, *bullet, int) {
|
|
for index, bullet := range g.bullets {
|
|
|
|
playerBullet := !bullet.enemy
|
|
if !playerBullet {
|
|
continue
|
|
}
|
|
distance := rl.Vector2Distance(e.pos, bullet.pos) - bullet.size
|
|
// fmt.Println(distance)
|
|
|
|
if distance < e.hitBoxRadius {
|
|
return true, bullet, index
|
|
}
|
|
}
|
|
return false, nil, 0
|
|
}
|
|
|
|
func (g *game) killEnemy(index int) {
|
|
g.enemies[index] = g.enemies[len(g.enemies)-1]
|
|
g.enemies = g.enemies[:len(g.enemies)-1]
|
|
}
|
|
|
|
|
|
func (e *enemy) update(g *game, index int) {
|
|
|
|
if e.movePattern != nil {
|
|
e.pos = e.movePattern(e)
|
|
}
|
|
|
|
if hit, bullet, idx := e.checkHit(g); hit {
|
|
e.health -= bullet.dmg
|
|
g.removeBullet(idx)
|
|
}
|
|
|
|
if e.health <= 0 {
|
|
g.killEnemy(index)
|
|
return
|
|
}
|
|
|
|
e.shoot(g)
|
|
|
|
rl.DrawCircleV(e.pos, 5, rl.Blue)
|
|
}
|
|
|
|
func (p *player) move() {
|
|
if rl.IsKeyDown(rl.KeyW) { p.pos.Y -= p.moveSpeed }
|
|
if rl.IsKeyDown(rl.KeyS) { p.pos.Y += p.moveSpeed }
|
|
if rl.IsKeyDown(rl.KeyA) { p.pos.X -= p.moveSpeed }
|
|
if rl.IsKeyDown(rl.KeyD) { p.pos.X += p.moveSpeed }
|
|
}
|
|
|
|
func (p *player) shoot(g *game){
|
|
if g.time % 10 != 0 {
|
|
return
|
|
}
|
|
|
|
if rl.IsMouseButtonDown(rl.MouseLeftButton) {
|
|
// não leva em consideração a velocidade do jogador
|
|
mouse := rl.GetMousePosition()
|
|
direction := rl.Vector2Subtract(mouse, p.pos)
|
|
direction = rl.Vector2Normalize(direction)
|
|
g.bullets = append(g.bullets, &bullet{
|
|
pos: p.pos,
|
|
size: 2,
|
|
speed: rl.Vector2Scale(direction, p.bulletMoveSpeed),
|
|
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
|
|
// fmt.Println(distance)
|
|
|
|
if distance < p.hitBoxRadius {
|
|
fmt.Println("hit!")
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *player) update(g *game) {
|
|
|
|
p.move()
|
|
|
|
p.shoot(g)
|
|
|
|
p.checkHit(g)
|
|
|
|
// hitbox
|
|
rl.DrawCircleV(p.pos, p.hitBoxRadius, rl.Red)
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
func main() {
|
|
|
|
state := &game{screenWidth: 450, screenHeight: 700}
|
|
|
|
player := player{
|
|
pos: rl.Vector2{X: 100, Y: 100},
|
|
moveSpeed: 4,
|
|
bulletMoveSpeed: 8,
|
|
hitBoxRadius: 5,
|
|
}
|
|
|
|
rl.SetTraceLog(rl.LogWarning | rl.LogDebug)
|
|
rl.InitWindow(state.screenWidth, state.screenHeight, "danmaku")
|
|
rl.SetTargetFPS(60)
|
|
|
|
state.enemies = []*enemy{
|
|
{
|
|
pos: rl.Vector2{X: 200, Y: 200},
|
|
health: 1,
|
|
hitBoxRadius: 5,
|
|
movePattern: horizonalPattern(state),
|
|
},
|
|
{
|
|
pos: rl.Vector2{X: 169, Y: 222},
|
|
health: 1,
|
|
hitBoxRadius:5,
|
|
movePattern: horizonalPattern(state),
|
|
},
|
|
{
|
|
pos: rl.Vector2{X: 400, Y: 400},
|
|
health: 1,
|
|
hitBoxRadius:5,
|
|
// movePattern: circlePattern(state, rl.Vector2{X: 200, Y: 200}),
|
|
},
|
|
{
|
|
pos: rl.Vector2{X: 200, Y: 200},
|
|
health: 1,
|
|
hitBoxRadius:5,
|
|
// movePattern: circlePattern(state, rl.Vector2{X: 200, Y: 200}),
|
|
}}
|
|
|
|
for ; !rl.WindowShouldClose(); state.time += 1 {
|
|
rl.BeginDrawing()
|
|
rl.ClearBackground(rl.Black)
|
|
|
|
rl.DrawText("danmaku", 20, 20, 20, rl.DarkGray)
|
|
rl.DrawLine(18, 42, state.screenWidth-18, 42, rl.Black)
|
|
rl.DrawFPS(0, 0)
|
|
|
|
player.update(state)
|
|
|
|
for i := range state.enemies {
|
|
state.enemies[i].update(state, i)
|
|
}
|
|
|
|
for i := 0; i < len(state.bullets); i++ {
|
|
state.bullets[i].update(state, i)
|
|
}
|
|
|
|
rl.EndDrawing()
|
|
}
|
|
|
|
rl.CloseWindow()
|
|
}
|