From fdacb476812643372f44b42f0d3dba497ea64504 Mon Sep 17 00:00:00 2001 From: silva guimaraes Date: Sun, 23 Jul 2023 00:19:54 -0300 Subject: [PATCH] resolver clipping --- main.go | 108 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index 0d1faa3..a3ad6d2 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "math" + "strconv" rl "github.com/gen2brain/raylib-go/raylib" ) @@ -16,11 +17,13 @@ const ( ) type game struct { - screenWidth int32 - screenHeight int32 + arenaWidth int32 + arenaHeight int32 + interfaceWidth int32 time int enemies []*enemy bullets []*bullet + score int } type bullet struct { @@ -33,9 +36,11 @@ type bullet struct { type player struct { pos rl.Vector2 + speed rl.Vector2 moveSpeed float32 bulletMoveSpeed float32 hitBoxRadius float32 + bulletSize float32 } type enemy struct { @@ -48,7 +53,7 @@ type enemy struct { 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) + v.Y <= float32(g.arenaHeight) && v.X <= float32(g.arenaWidth) } func (g *game) removeBullet(index int) { @@ -71,8 +76,6 @@ 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, @@ -148,13 +151,17 @@ func (g *game) killEnemy(index int) { func (e *enemy) update(g *game, index int) { + enemyColor := rl.Blue + if e.movePattern != nil { e.pos = e.movePattern(e) } if hit, bullet, idx := e.checkHit(g); hit { + g.score += 273 e.health -= bullet.dmg g.removeBullet(idx) + enemyColor = rl.White } if e.health <= 0 { @@ -164,30 +171,54 @@ func (e *enemy) update(g *game, index int) { e.shoot(g) - rl.DrawCircleV(e.pos, 5, rl.Blue) + rl.DrawCircleV(e.pos, e.hitBoxRadius, enemyColor) } -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) move(g *game) { + 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, p.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 g.time % 10 != 0 { + if g.time % 5 != 0 { return } if rl.IsMouseButtonDown(rl.MouseLeftButton) { - // não leva em consideração a velocidade do jogador + // (mal) leva em consideração a velocidade do jogador 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: 2, - speed: rl.Vector2Scale(direction, p.bulletMoveSpeed), + size: p.bulletSize, + speed: direction, dmg: 1, enemy: false, }) @@ -210,7 +241,7 @@ func (p *player) checkHit(g *game) { func (p *player) update(g *game) { - p.move() + p.move(g) p.shoot(g) @@ -231,56 +262,60 @@ func (p *player) update(g *game) { func main() { - state := &game{screenWidth: 450, screenHeight: 700} + state := &game{arenaWidth: 450, arenaHeight: 700, interfaceWidth: 300} player := player{ pos: rl.Vector2{X: 100, Y: 100}, moveSpeed: 4, - bulletMoveSpeed: 8, + bulletMoveSpeed: 13, + bulletSize: 8, hitBoxRadius: 5, } rl.SetTraceLog(rl.LogWarning | rl.LogDebug) - rl.InitWindow(state.screenWidth, state.screenHeight, "danmaku") + rl.InitWindow(state.arenaWidth + state.interfaceWidth, state.arenaHeight, "danmaku") rl.SetTargetFPS(60) state.enemies = []*enemy{ { pos: rl.Vector2{X: 200, Y: 200}, - health: 1, - hitBoxRadius: 5, + health: 10, + hitBoxRadius: 10, movePattern: horizonalPattern(state), }, { pos: rl.Vector2{X: 169, Y: 222}, - health: 1, - hitBoxRadius:5, + health: 10, + hitBoxRadius:10, movePattern: horizonalPattern(state), }, { pos: rl.Vector2{X: 400, Y: 400}, - health: 1, - hitBoxRadius:5, + health: 10, + hitBoxRadius:10, // movePattern: circlePattern(state, rl.Vector2{X: 200, Y: 200}), }, { pos: rl.Vector2{X: 200, Y: 200}, - health: 1, - hitBoxRadius:5, + health: 10, + hitBoxRadius:10, // movePattern: circlePattern(state, rl.Vector2{X: 200, Y: 200}), - }} + }, + } + + currectScore := 0 for ; !rl.WindowShouldClose(); state.time += 1 { - rl.BeginDrawing() - rl.ClearBackground(rl.Black) + 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.DrawText("danmaku babe bullet shoot shoot", 20, 20, 20, rl.DarkGray) + rl.DrawLine(18, 42, state.arenaWidth-18, 42, rl.Black) rl.DrawFPS(0, 0) player.update(state) - for i := range state.enemies { + for i := 0; i < len(state.enemies); i++ { state.enemies[i].update(state, i) } @@ -288,6 +323,13 @@ func main() { state.bullets[i].update(state, i) } + rl.DrawRectangle( + state.arenaWidth, 0, state.interfaceWidth, state.arenaHeight, + rl.NewColor(0, 33, 59, 255), + ) + currectScore += (state.score - currectScore) / 11 + rl.DrawText(strconv.Itoa(currectScore), state.arenaWidth, 0, 50, rl.White) + rl.EndDrawing() }