Skip to content

Commit e64b93a

Browse files
committed
reworked hierarchy system
also added a few more testing objects
1 parent 9a90b6b commit e64b93a

File tree

2 files changed

+54
-59
lines changed

2 files changed

+54
-59
lines changed

engine.js

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,43 @@ export function sprite(options) {
2525
this.texture
2626
this.size = options.size
2727

28-
this.imReady = function() {
29-
sprite.ready = true
30-
sprite.render()
31-
console.log(options.texture +" ready, rendering children")
32-
for (const child of sprite.children) {
33-
child.render()
28+
this.render = function() {
29+
if (!sprite.parent) {
30+
console.log(sprite.texture +" tried rendering but couldnt")
31+
return
32+
} // Stop if theres no parent
33+
if (!sprite.ready || !sprite.parent.ready) {return} // Stop if texture or parent's texture isnt ready
34+
// Sets the center of the element to where it's top left corner was
35+
// Then sets its center to the center of its parent
36+
this.body.style.position = "absolute"
37+
this.body.style.left = -this.size.x/2 + nopx(this.parent.body.style.width)/2 +"px"
38+
this.body.style.top = -this.size.y/2 + nopx(this.parent.body.style.height)/2 +"px"
39+
40+
this.body.style.transform = ""
41+
if (this.rot.p) {
42+
this.body.style.transformStyle = "preserve-3d"
43+
this.body.style.transform += "perspective("+ this.rot.p +"px) "
44+
}
45+
this.body.style.transform += "translate("+ this.pos.x +"px, "+ this.pos.y +"px) " // X Y
46+
this.body.style.transform += "rotateX("+ this.rot.x +"deg) " // Rotation X
47+
this.body.style.transform += "rotateY("+ this.rot.y +"deg) " // Rotation Y
48+
this.body.style.transform += "rotateZ("+ this.rot.z +"deg) " // Rotation Z (2D)
49+
this.body.style.transformStyle = ""
50+
this.body.style.transform += "scale("+ this.scale +") " // Scale
51+
//this.body.style.transformOrigin = "bottom"
52+
53+
this.body.style.width = this.size.x +"px" // Width
54+
this.body.style.height = this.size.y +"px" // Height
55+
if (this.texture != null) {
56+
this.body.style.background = "url("+ this.texture.src +")"
3457
}
58+
59+
this.body.style.transform += "translate("+ this.offset.x +"px, "+ this.offset.y +"px) " // Offset
60+
//this.body.style.transform += "translateZ(0) "
61+
this.body.style.transformOrigin = (-this.offset.x+this.body.style.width/2) +"px "+ (-this.offset.y+this.body.style.height/2) +"px"
62+
this.body.style.imageRendering = "pixelated"
63+
64+
this.body.style.outline = this.debug ? "1px solid yellow" : "none"
3565
}
3666

3767
this.setTexture = function(url) {
@@ -51,21 +81,32 @@ export function sprite(options) {
5181
} else {
5282
sprite.texture = null
5383
if (sprite.size == null) {sprite.size = {x:0, y:0}}
84+
sprite.imReady()
5485
}
5586
}
5687

5788
this.append = function(child) {
5889
sprite.children.push(child)
5990
child.setParent(sprite)
91+
child.render()
6092
}
6193
this.setParent = function(parent) {
6294
if (sprite.parent) {
6395
sprite.parent.children = sprite.parent.children.filter(element => element != sprite)
6496
}
6597
sprite.parent = parent
6698
parent.body.appendChild(this.body)
99+
sprite.render()
100+
}
101+
this.imReady = function() {
102+
sprite.ready = true
103+
sprite.render()
104+
console.log(options.texture +" ready, rendering children:")
105+
for (const child of sprite.children) {
106+
child.render()
107+
console.log("- "+ child.texture)
108+
}
67109
}
68-
69110

70111
if (options.texture != "canvas") { // Normal behaviour
71112
this.setTexture(options.texture)
@@ -74,7 +115,7 @@ export function sprite(options) {
74115
this.body = document.createElement("canvas")
75116
this.body.width = this.size.x
76117
this.body.height = this.size.y
77-
this.ready = true
118+
sprite.imReady()
78119
}
79120

80121
this.body.addEventListener("click", (event) => {
@@ -112,43 +153,5 @@ export function sprite(options) {
112153
this.render()
113154
}
114155

115-
this.render = function() {
116-
if (!sprite.ready || !sprite.parent.ready) {return}
117-
// Sets the center of the element to where it's top left corner was
118-
// Then sets its center to the center of its parent
119-
this.body.style.position = "absolute"
120-
this.body.style.left = -this.size.x/2 + nopx(this.parent.body.style.width)/2 +"px"
121-
this.body.style.top = -this.size.y/2 + nopx(this.parent.body.style.height)/2 +"px"
122-
123-
this.body.style.transform = ""
124-
if (this.rot.p) {
125-
this.body.style.transformStyle = "preserve-3d"
126-
this.body.style.transform += "perspective("+ this.rot.p +"px) "
127-
}
128-
this.body.style.transform += "translate("+ this.pos.x +"px, "+ this.pos.y +"px) " // X Y
129-
this.body.style.transform += "rotateX("+ this.rot.x +"deg) " // Rotation X
130-
this.body.style.transform += "rotateY("+ this.rot.y +"deg) " // Rotation Y
131-
this.body.style.transform += "rotateZ("+ this.rot.z +"deg) " // Rotation Z (2D)
132-
this.body.style.transformStyle = ""
133-
this.body.style.transform += "scale("+ this.scale +") " // Scale
134-
//this.body.style.transformOrigin = "bottom"
135-
136-
this.body.style.width = this.size.x +"px" // Width
137-
this.body.style.height = this.size.y +"px" // Height
138-
if (this.texture != null) {
139-
this.body.style.background = "url("+ this.texture.src +")"
140-
}
141-
142-
this.body.style.transform += "translate("+ this.offset.x +"px, "+ this.offset.y +"px) " // Offset
143-
//this.body.style.transform += "translateZ(0) "
144-
this.body.style.transformOrigin = (-this.offset.x+this.body.style.width/2) +"px "+ (-this.offset.y+this.body.style.height/2) +"px"
145-
this.body.style.imageRendering = "pixelated"
146-
147-
this.body.style.filter = "drop-shadow(5px 5px 5px #222)"
148-
this.body.style.filter = "none"
149-
//console.log(this.body.style)
150-
151-
this.body.style.border = this.debug ? "1px solid yellow" : "none"
152-
}
153156
//this.render()
154157
}

script.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,12 @@ window.onload = function() {
5555
offset: {
5656
x: 0,
5757
y: 0
58-
}
58+
},
59+
texture: "botWalk.gif"
5960
})
6061

6162
world.setParent(cam)
62-
6363
world.append(child)
64-
console.log(world.children)
65-
/* world.append(child)
66-
console.log(world.children)
67-
world.append(child)
68-
console.log(world.children) */
6964

7065
const orbit = new sprite({
7166
texture: "placeholder.png",
@@ -76,15 +71,12 @@ window.onload = function() {
7671
}
7772
})
7873
child.append(orbit)
79-
80-
/*
8174

8275
const planet = new sprite({
83-
parent: child,
8476
texture: "canvas",
8577
pos: {
8678
x: 0,
87-
y: 0
79+
y: 50
8880
},
8981
size: {
9082
x: 32,
@@ -95,12 +87,12 @@ window.onload = function() {
9587
planet.render()
9688
}
9789
})
98-
console.log(planet)
90+
child.append(planet)
9991
const ctx = planet.body.getContext("2d")
10092
ctx.fillStyle = "rgb(200 0 0 / 25%)";
10193
ctx.fillRect(0, 0, 32, 32);
10294
ctx.fillStyle = "rgb(0 0 200)";
103-
ctx.fillRect(15, 15, 2, 2) */
95+
ctx.fillRect(15, 15, 2, 2)
10496

10597
const canvas = document.getElementById("canvas")
10698
canvas.style.width = window.innerWidth +"px"

0 commit comments

Comments
 (0)