ballsoffline/bf.go

195 lines
4.8 KiB
Go

package main
import (
"fmt"
"strconv"
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
screenWidth = 1280
screenHeight = 720
ballRadius = 12
debug = false
)
var (
ballpos = rl.Vector2{X: 400, Y: 225}
ballColor = rl.Black
speed = 3
squareSize = 100
squares = []rl.Vector2{{X: 640 - float32(squareSize), Y: 315}, {X: 640, Y: 315}, {X: 640 + float32(squareSize), Y: 315}, {X: 640, Y: 315 - float32(squareSize)}, {X: 640, Y: 315 - float32(squareSize)*2}}
paints = []paint{}
)
func main() {
userData := loadJson("account.json")
checkUserDataValid(userData)
ballColor = convertHex(userData["color"].(string))
rl.InitWindow(screenWidth, screenHeight, "balls offline")
guessy4 := rl.LoadFont("guessy4.ttf")
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.NewColor(128, 128, 128, 255))
drawSquares()
drawPaint()
drawPosition(ballpos, rl.Vector2{X: 10, Y: screenHeight - 20}, guessy4)
drawBall(ballpos, ballColor, rl.White)
moveBall()
drawTextShadow(userData["username"].(string), rl.Vector2{X: ballpos.X - ballRadius - 24, Y: ballpos.Y - ballRadius - 40}, guessy4)
rl.EndDrawing()
}
rl.CloseAudioDevice()
rl.CloseWindow()
}
func drawPaint() {
if debug {
t := strconv.Itoa(len(paints))
rl.DrawText(t, 10, 10, 40, rl.Black)
}
for index, paintI := range paints {
if paintI.time > 1 {
paints[index] = paint{time: paintI.time - 1, location: paintI.location}
rl.DrawRectangleV(paintI.location, rl.Vector2{X: ballRadius - 2, Y: ballRadius - 2}, rl.NewColor(ballColor.R, ballColor.G, ballColor.R, uint8(paintI.time)))
} else {
removePaint(paints, index)
}
}
}
func drawSquares() {
for index, element := range squares {
if debug {
fmt.Println(index, element.X)
}
rl.DrawRectangleV(element, rl.Vector2{X: float32(squareSize), Y: float32(squareSize)}, rl.LightGray)
}
}
func draw() {
paintedPaint := paint{255, ballpos}
paints = append(paints, paintedPaint)
}
func moveBall() {
if rl.IsKeyDown(rl.KeyLeftShift) {
draw()
}
if rl.IsKeyDown(rl.KeyS) {
speed += 2
}
if rl.IsKeyDown(rl.KeyRight) {
if !checkCollision(rl.Vector2{X: ballpos.X + float32(speed), Y: ballpos.Y}) {
ballpos.X = ballpos.X + float32(speed)
}
}
if rl.IsKeyDown(rl.KeyLeft) {
if !checkCollision(rl.Vector2{X: ballpos.X - float32(speed), Y: ballpos.Y}) {
ballpos.X = ballpos.X - float32(speed)
}
}
if rl.IsKeyDown(rl.KeyUp) {
if !checkCollision(rl.Vector2{X: ballpos.X, Y: ballpos.Y - float32(speed)}) {
ballpos.Y = ballpos.Y - float32(speed)
}
}
if rl.IsKeyDown(rl.KeyDown) {
if !checkCollision(rl.Vector2{X: ballpos.X, Y: ballpos.Y + float32(speed)}) {
ballpos.Y = ballpos.Y + float32(speed)
}
}
}
func drawBall(pos rl.Vector2, color, outline rl.Color) {
rl.DrawCircleV(pos, ballRadius+1, outline)
rl.DrawCircleV(pos, ballRadius, color)
}
func drawPosition(pos, textPos rl.Vector2, guessy4 rl.Font) {
x := strconv.FormatFloat(float64(pos.X), 'f', -1, 64)
y := strconv.FormatFloat(float64(pos.Y), 'f', -1, 64)
drawTextShadow("X:"+x, textPos, guessy4)
drawTextShadow("Y:"+y, rl.Vector2Add(textPos, rl.Vector2{X: 90, Y: 0}), guessy4)
}
func drawTextShadow(text string, textPos rl.Vector2, guessy4 rl.Font) {
var textShadowPos rl.Vector2 = rl.Vector2{X: textPos.X + 2, Y: textPos.Y + 2}
rl.DrawTextEx(guessy4, text, textShadowPos, 20, 1, rl.Black)
rl.DrawTextEx(guessy4, text, textPos, 20, 1, rl.White)
}
func checkCollision(newPos rl.Vector2) bool {
if debug {
rl.DrawLineV(newPos, rl.Vector2{X: newPos.X, Y: screenWidth * 2}, rl.Green)
rl.DrawLineV(newPos, rl.Vector2{X: newPos.X, Y: 0}, rl.Green)
rl.DrawLineV(newPos, rl.Vector2{X: 0, Y: newPos.Y}, rl.Green)
rl.DrawLineV(newPos, rl.Vector2{X: screenHeight * 2, Y: newPos.Y}, rl.Green)
}
for index, square := range squares {
if debug {
fmt.Println(index, square.X, rl.Vector2Distance(newPos, square))
rl.DrawLineV(newPos, square, rl.Purple)
}
if rl.CheckCollisionPointRec(newPos, rl.Rectangle{X: square.X, Y: square.Y, Width: float32(squareSize), Height: float32(squareSize)}) {
if debug {
rl.DrawText("colliding with obstacle", 10, 10, 40, rl.Black)
}
return true
}
}
if newPos.X > screenWidth-12 {
return true
}
if newPos.X < 12 {
return true
}
if newPos.Y < 12 {
return true
}
if newPos.Y > screenHeight-12 {
return true
}
return false
}
type paint struct {
time float32
location rl.Vector2
}
func removePaint(s []paint, i int) []paint {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
func convertHex(hex string) rl.Color {
hex1 := hex[0:2]
hex2 := hex[2:4]
hex3 := hex[4:6]
decimal, err := strconv.ParseInt(hex1, 16, 64)
decimal2, err := strconv.ParseInt(hex2, 16, 64)
decimal3, err := strconv.ParseInt(hex3, 16, 64)
if err != nil {
fmt.Println(err)
}
return rl.NewColor(uint8(decimal), uint8(decimal2), uint8(decimal3), 255)
}