commit inicial
This commit is contained in:
commit
aa2bd3fbfd
3 changed files with 196 additions and 0 deletions
5
go.mod
Normal file
5
go.mod
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
module danmaku
|
||||||
|
|
||||||
|
go 1.20
|
||||||
|
|
||||||
|
require github.com/gen2brain/raylib-go/raylib v0.0.0-20230719211022-1083eace2049
|
||||||
2
go.sum
Normal file
2
go.sum
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
github.com/gen2brain/raylib-go/raylib v0.0.0-20230719211022-1083eace2049 h1:fJeDPYJDyT+KeWKsIMbRlNcSgsQ2nZ4bJLwBraVA/xU=
|
||||||
|
github.com/gen2brain/raylib-go/raylib v0.0.0-20230719211022-1083eace2049/go.mod h1:AwtGA3aTtYdezNxEVbfchaLw/z+CuRDh2Mlxy0FbBro=
|
||||||
189
main.go
Normal file
189
main.go
Normal file
|
|
@ -0,0 +1,189 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
type enemy struct {
|
||||||
|
pos rl.Vector2
|
||||||
|
health int
|
||||||
|
movePattern movementPattern
|
||||||
|
shootPattern shootingPattern
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (b *bullet) update(g *game, index int) {
|
||||||
|
b.pos = rl.Vector2Add(b.pos, b.speed)
|
||||||
|
|
||||||
|
if !g.insideArena(b.pos) {
|
||||||
|
g.bullets[index] = g.bullets[len(g.bullets)-1]
|
||||||
|
g.bullets = g.bullets[:len(g.bullets)-1]
|
||||||
|
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 (e *enemy) update(g *game) {
|
||||||
|
rl.DrawCircleV(e.pos, 5, rl.Blue)
|
||||||
|
e.shoot(g)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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 < 0 {
|
||||||
|
fmt.Println("hit!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *player) update(g *game) {
|
||||||
|
|
||||||
|
p.move()
|
||||||
|
|
||||||
|
p.shoot(g)
|
||||||
|
|
||||||
|
p.checkHit(g)
|
||||||
|
|
||||||
|
// hitbox
|
||||||
|
rl.DrawCircleV(p.pos, 5, 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.Red)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
state := &game{screenWidth: 450, screenHeight: 700}
|
||||||
|
|
||||||
|
player := player{
|
||||||
|
pos: rl.Vector2{X: 100, Y: 100},
|
||||||
|
moveSpeed: 4,
|
||||||
|
bulletMoveSpeed: 8,
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.SetTraceLog(rl.LogWarning | rl.LogDebug)
|
||||||
|
rl.InitWindow(state.screenWidth, state.screenHeight, "danmaku")
|
||||||
|
rl.SetTargetFPS(60)
|
||||||
|
|
||||||
|
state.enemies = append(state.enemies, &enemy{
|
||||||
|
pos: rl.Vector2{X: 200, Y: 200},
|
||||||
|
health: 10,
|
||||||
|
})
|
||||||
|
state.enemies = append(state.enemies, &enemy{
|
||||||
|
pos: rl.Vector2{X: 169, Y: 222},
|
||||||
|
health: 10,
|
||||||
|
})
|
||||||
|
state.enemies = append(state.enemies, &enemy{
|
||||||
|
pos: rl.Vector2{X: 219, Y: 195},
|
||||||
|
health: 10,
|
||||||
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(state.bullets); i++ {
|
||||||
|
state.bullets[i].update(state, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.EndDrawing()
|
||||||
|
}
|
||||||
|
|
||||||
|
rl.CloseWindow()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue