Files
prosperon/sprite.cm
2025-12-29 20:46:42 -06:00

107 lines
2.4 KiB
Plaintext

// sprite.cm - Sprite node factory
//
// Returns a function that creates sprite instances via meme()
var sprite = {
type: 'sprite',
pos: null,
layer: 0,
image: null,
width: 1,
height: 1,
anchor_x: 0,
anchor_y: 0,
scale_x: 1,
scale_y: 1,
color: null,
uv_rect: null,
slice: null,
tile: null,
material: null,
animation: null,
frame: 0,
opacity: 1,
// Dirty tracking
dirty: 7, // DIRTY.ALL
// Cached geometry (for retained mode)
geom_cache: null,
// Setters that mark dirty
set_pos: function(x, y) {
if (!this.pos) this.pos = {x: 0, y: 0}
if (this.pos.x == x && this.pos.y == y) return this
this.pos.x = x
this.pos.y = y
this.dirty |= 1 // TRANSFORM
return this
},
set_image: function(img) {
if (this.image == img) return this
this.image = img
this.dirty |= 2 // CONTENT
return this
},
set_size: function(w, h) {
if (this.width == w && this.height == h) return this
this.width = w
this.height = h
this.dirty |= 2 // CONTENT
return this
},
set_anchor: function(x, y) {
if (this.anchor_x == x && this.anchor_y == y) return this
this.anchor_x = x
this.anchor_y = y
this.dirty |= 1 // TRANSFORM
return this
},
set_color: function(r, g, b, a) {
if (!this.color) this.color = {r: 1, g: 1, b: 1, a: 1}
if (this.color.r == r && this.color.g == g && this.color.b == b && this.color.a == a) return this
this.color.r = r
this.color.g = g
this.color.b = b
this.color.a = a
this.dirty |= 2 // CONTENT
return this
},
set_opacity: function(o) {
if (this.opacity == o) return this
this.opacity = o
this.dirty |= 2 // CONTENT
return this
}
}
// Factory function
return function(props) {
var s = meme(sprite)
s.pos = {x: 0, y: 0}
s.color = {r: 1, g: 1, b: 1, a: 1}
s.dirty = 7
if (props) {
for (var k in props) {
if (k == 'pos' && props.pos) {
s.pos.x = props.pos.x || 0
s.pos.y = props.pos.y || 0
} else if (k == 'color' && props.color) {
s.color.r = props.color.r != null ? props.color.r : 1
s.color.g = props.color.g != null ? props.color.g : 1
s.color.b = props.color.b != null ? props.color.b : 1
s.color.a = props.color.a != null ? props.color.a : 1
} else {
s[k] = props[k]
}
}
}
return s
}