This commit is contained in:
2026-01-06 20:25:55 -06:00
parent 50bee7a5c0
commit 0522b967ca
21 changed files with 724 additions and 1244 deletions

View File

@@ -1,85 +1,71 @@
// sprite.cm - Sprite node factory
//
// Returns a function that creates sprite instances via meme()
var film2d = use('film2d')
var dirty = {}
var sprite = {
// Setters that mark dirty
var sprite_proto = {
type: 'sprite',
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
set_groups: function(groups) {
var old_groups = this.groups
this.groups = groups
film2d.reindex(this._id, old_groups, groups)
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
add_group: function(group) {
if (this.groups.indexOf(group) < 0) {
this.groups.push(group)
film2d.index_group(this._id, group)
}
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
remove_group: function(group) {
var idx = this.groups.indexOf(group)
if (idx >= 0) {
this.groups.splice(idx, 1)
film2d.unindex_group(this._id, group)
}
return this
},
set_color: function(color) {
if (!this.color) this.color = {r: 1, g: 1, b: 1, a: 1}
if (this.color.r == color.r && this.color.g == color.g && this.color.b == color.b && this.color.a == color.a) return this
this.color.r = color.r
this.color.g = color.g
this.color.b = color.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
destroy: function() {
film2d.unregister(this._id)
}
}
stone(sprite)
// Factory function
return function(props) {
var defaults = {
pos: {x:0,y:0},
layer: 0,
type: 'sprite',
pos: {x: 0, y: 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,
flip_x: false,
flip_y: false,
rotation: 0,
color: {r: 1, g: 1, b: 1, a: 1},
opacity: 1,
layer: 0,
groups: ['default'],
visible: true
}
var newsprite = meme(sprite, [defaults,props])
newsprite[dirty] = 7
return newsprite
var data = {}
for (var k in defaults) data[k] = defaults[k]
for (var k in props) data[k] = props[k]
// Ensure groups is array
if (!data.groups) data.groups = ['default']
if (is_text(data.groups)) data.groups = [data.groups]
var s = meme(sprite_proto, data)
film2d.register(s)
return s
}