Merge pull request 'fix deleting array element' (#4) from mertoalex/ballsoffline:main into main

Reviewed-on: gnawmon/ballsoffline#4
This commit is contained in:
gnawmon 2024-04-25 15:13:23 +00:00
commit 373597a3c2
4 changed files with 42 additions and 31 deletions

49
bf.go
View File

@ -59,22 +59,22 @@ func drawPaint() {
rl.DrawText(t, 10, 10, 40, rl.Black)
}
for index, paintI := range paints {
if len(paints) <= index {
continue
}
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)
continue
}
paints = removePaint(paints, index)
}
}
func drawSquares() {
for index, element := range squares {
if debug {
fmt.Println(index, element.X)
}
debugLog(debug, fmt.Sprintln(index, element.X))
rl.DrawRectangleV(element, rl.Vector2{X: float32(squareSize), Y: float32(squareSize)}, rl.LightGray)
}
}
@ -90,25 +90,17 @@ func moveBall() {
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.KeyRight) && !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.KeyLeft) && !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)}) {
if rl.IsKeyDown(rl.KeyUp) && !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)
}
if rl.IsKeyDown(rl.KeyDown) && !checkCollision(rl.Vector2{X: ballpos.X, Y: ballpos.Y + float32(speed)}) {
ballpos.Y = ballpos.Y + float32(speed)
}
}
@ -173,9 +165,11 @@ type paint struct {
location rl.Vector2
}
func removePaint(s []paint, i int) []paint {
s[i] = s[len(s)-1]
return s[:len(s)-1]
func removePaint(slice []paint, index int) []paint {
if len(slice) <= 2 {
return make([]paint, 0)
}
return append(slice[:index], slice[index+1:]...)
}
func convertHex(hex string) rl.Color {
@ -183,12 +177,11 @@ func convertHex(hex string) rl.Color {
hex2 := hex[2:4]
hex3 := hex[4:6]
decimal, err := strconv.ParseInt(hex1, 16, 64)
checkLog(err)
decimal2, err := strconv.ParseInt(hex2, 16, 64)
checkLog(err)
decimal3, err := strconv.ParseInt(hex3, 16, 64)
if err != nil {
fmt.Println(err)
}
checkLog(err)
return rl.NewColor(uint8(decimal), uint8(decimal2), uint8(decimal3), 255)
}

13
debughandler.go Normal file
View File

@ -0,0 +1,13 @@
package main
import "fmt"
func debugLog(debug bool, print string) {
if debug {
fmt.Println(print)
}
}
/*
TODO: add other debuging code here so it's be more simple to read bf.go
*/

View File

@ -8,6 +8,12 @@ func check(Error error) {
}
}
func checkLog(Error error) {
if Error != nil {
fmt.Println("error:", Error)
}
}
func checkValid(something any) bool {
if something == nil {
return false
@ -15,7 +21,7 @@ func checkValid(something any) bool {
return true
}
func makeDefault(variable *map[string]interface{}, Type string) {
func setDefault(variable *map[string]interface{}, Type string) {
switch Type {
case "color": (*variable)[Type] = "000000"
default: panic(fmt.Sprint("\"", Type, "\" isn't a type!"))
@ -34,7 +40,7 @@ func checkUserDataValid(userdata map[string]interface{}) {
for _, data := range []string{"color"} {
if !checkValid(userdata[data]) {
fmt.Printf("WARNING: \"%s\" isn't defined in userdata:\n\t%s\n\tgonna use default variable for that\n", data, userdata)
makeDefault(&userdata, data)
setDefault(&userdata, data)
}
}
}

View File

@ -12,4 +12,3 @@ func loadJson(file string) map[string]interface{} {
check(json.Unmarshal([]byte(jsonRaw), &jsonData))
return jsonData
}