1
0
Fork 0

auts: depth-sort sprites in spritesys

This commit is contained in:
coaljoe 2020-08-24 00:05:20 +03:00
parent c2cd0716d1
commit 25a5d5f5a2
1 changed files with 41 additions and 0 deletions

View File

@ -4,6 +4,24 @@ interface Drawable {
draw()
}
fn depth_compare(a, b &Sprite) int {
//println(a.y)
//println(b.y)
if a.y < b.y {
//println("ret -1")
return -1
}
if a.y > b.y {
//println("ret 1")
return 1
}
//println("ret 0")
//pp(4)
return 0
}
struct SpriteSys {
mut:
sys &System // Embed
@ -32,6 +50,26 @@ fn (mut s SpriteSys) draw() {
// XXX TODO sort sprites by z
//mut sorted_elems := s.sys.elems.clone()
//sorted_elems.sort_with_compare(depth_compare)
mut sorted_sprites := []Sprite{}
for el in s.sys.elems {
mut x := &Sprite(el)
sorted_sprites << x
}
sorted_sprites.sort_with_compare(depth_compare)
for x in sorted_sprites {
mut z := x
z.draw()
//println(z.y)
}
//pp(3)
/*
for el in s.sys.elems {
//el.draw()
//x := Drawable{el}
@ -39,7 +77,10 @@ fn (mut s SpriteSys) draw() {
//mut x := &Drawable(el)
mut x := &Sprite(el)
x.draw()
println(x.y)
}
*/
}
fn (mut s SpriteSys) update(dt f32) {