fixing paint array deleting

This commit is contained in:
mertoalex 2024-04-24 01:12:56 +03:00
parent 68abf22ca9
commit 339425507a
3 changed files with 25 additions and 13 deletions

27
bf.go
View File

@ -21,6 +21,7 @@ var (
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{}
test = 0
)
func main() {
@ -59,14 +60,19 @@ 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
}
fmt.Println("end", test)
test = test + 1
paints = removePaint(paints, index)
}
}
}
func drawSquares() {
@ -173,9 +179,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 +191,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)
}

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
}