Update framework + usage
@@ -114,6 +114,7 @@ class GameObject
114
114
{
115
115
this.transform.update(deltaTime);
116
116
this.collider.update(deltaTime);
117
this.renderer.update(deltaTime);
117
118
}
118
119
119
120
destroy()
@@ -142,34 +143,29 @@ class GameObject
142
143
* Start File:
143
144
* /c/Users/Nick/source/repos/frameworkv3/framework/engine/image.js
144
145
*/
145
function image(image, subImgTotal, perRow)
146
{
147
subImgTotal = subImgTotal || 1;
148
perRow = perRow || 1;
146
class image{
147
subImages = null;
148
subImagesPerRow = null;
149
149
150
this.subImages = subImgTotal;
151
this.subImagesPerRow = perRow;
152
this.g_image = image;
153
image.id="loadedImage";
154
document.body.append(image);
150
g_image = null;
155
151
156
Object.defineProperty(this, "width",
157
{
158
get: function()
159
{
160
return this.g_image.width / this.subImagesPerRow;
161
}
162
});
152
animationCycles = {};
163
153
164
Object.defineProperty(this, "height",
154
constructor(image, subImgTotal = 1, perRow = 1)
165
155
{
166
get: function()
167
{
168
return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
169
}
170
});
156
subImgTotal = subImgTotal || 1;
157
perRow = perRow || 1;
158
159
this.subImages = subImgTotal;
160
this.subImagesPerRow = perRow;
161
this.g_image = image;
171
162
172
this.draw = function(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
163
image.id="loadedImage";
164
document.body.append(image);
165
console.log("Image loaded: " + image.src);
166
}
167
168
draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha)
173
169
{
174
170
var shouldDraw = true;
175
171
@@ -195,14 +191,29 @@ function image(image, subImgTotal, perRow)
195
191
context.translate(dx, dy);
196
192
context.rotate(rotation * (Math.PI/180));
197
193
198
context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
194
(mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1);
199
195
196
context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
197
200
198
context.restore();
201
199
context.globalAlpha = oldAlpha;
202
200
}
203
201
}
204
202
205
console.log("Image loaded: " + image.src);
203
addAnimationCycle(label, cycle)
204
{
205
this.animationCycles[label] = cycle;
206
}
207
208
get width()
209
{
210
return this.g_image.width / this.subImagesPerRow;
211
}
212
213
get height()
214
{
215
return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
216
}
206
217
}
207
218
/*
208
219
* End File:
@@ -349,6 +360,10 @@ class Renderer
349
360
gameObject = null;
350
361
subImage = 0;
351
362
alpha = 1;
363
mirrorX = false;
364
mirrorY = false;
365
366
cycle = null;
352
367
353
368
visible = true;
354
369
@@ -358,6 +373,34 @@ class Renderer
358
373
this.sprite = sprite;
359
374
}
360
375
376
update(deltaTime)
377
{
378
if(this.cycle)
379
{
380
if(this.cycle.cycleSpeed && this.cycle.cycleSpeed == -1)
381
{
382
this.subImage = this.cycle.default;
383
this.cycle.current = this.cycle.default;
384
return;
385
}
386
this.cycle.timer += deltaTime;
387
388
if(this.cycle.timer > this.cycle.cycleSpeed)
389
{
390
this.cycle.timer -= this.cycle.cycleSpeed;
391
392
this.cycle.cycleArray[this.cycle.current + 1] ?
393
this.updateCycleSprite(this.cycle.current + 1) :
394
this.updateCycleSprite(0);
395
}
396
}
397
}
398
399
updateCycleSprite(index) {
400
this.subImage = this.cycle.cycleArray[index];
401
this.cycle.current = index;
402
}
403
361
404
draw()
362
405
{
363
406
if(this.sprite)
@@ -402,9 +445,47 @@ class Renderer
402
445
this.gameObject.transform.size.x,
403
446
this.gameObject.transform.size.y,
404
447
this.gameObject.transform.rotation,
448
this.mirrorX,
449
this.mirrorY,
405
450
this.alpha
406
451
);
407
452
}
453
454
setAnimation(label, defaultIndex, cycleSpeed)
455
{
456
if(this.cycle)
457
{
458
if(this.cycle.label == label)
459
{
460
this.cycle.defaultIndex = defaultIndex;
461
this.cycle.cycleSpeed = cycleSpeed;
462
}
463
}
464
else
465
{
466
this.cycle = {
467
label: label,
468
cycleArray: this.sprite.animationCycles[label],
469
default: defaultIndex,
470
current: defaultIndex,
471
cycleSpeed: cycleSpeed,
472
timer: 0};
473
}
474
}
475
476
setAnimationCycleSpeed(cycleSpeed)
477
{
478
if(this.cycle)
479
{
480
this.cycle.cycleSpeed = cycleSpeed;
481
}
482
}
483
484
setSubImage(subImage)
485
{
486
this.subImage = subImage;
487
this.cycle = null;
488
}
408
489
}
409
490
/*
410
491
* End File:
@@ -114,6 +114,7 @@ class GameObject114
{114
{115
this.transform.update(deltaTime);115
this.transform.update(deltaTime);116
this.collider.update(deltaTime);116
this.collider.update(deltaTime);117
this.renderer.update(deltaTime);117
}118
}118
119
119
destroy()120
destroy()@@ -142,34 +143,29 @@ class GameObject142
* Start File:143
* Start File:143
* /c/Users/Nick/source/repos/frameworkv3/framework/engine/image.js144
* /c/Users/Nick/source/repos/frameworkv3/framework/engine/image.js144
*/ 145
*/ 145
function image(image, subImgTotal, perRow)146
class image{146
{147
subImages = null;147
subImgTotal = subImgTotal || 1;148
subImagesPerRow = null;148
perRow = perRow || 1;149
149
150
this.subImages = subImgTotal;150
g_image = null;151
this.subImagesPerRow = perRow;152
this.g_image = image;153
image.id="loadedImage";154
document.body.append(image);155
151
156
Object.defineProperty(this, "width",152
animationCycles = {};157
{158
get: function()159
{160
return this.g_image.width / this.subImagesPerRow;161
}162
});163
153
164
Object.defineProperty(this, "height",154
constructor(image, subImgTotal = 1, perRow = 1)165
{155
{166
get: function()156
subImgTotal = subImgTotal || 1;167
{157
perRow = perRow || 1;168
return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);158
169
}159
this.subImages = subImgTotal;170
});160
this.subImagesPerRow = perRow;161
this.g_image = image;171
162
172
this.draw = function(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)163
image.id="loadedImage";164
document.body.append(image);165
console.log("Image loaded: " + image.src);166
}167
168
draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha)173
{ 169
{ 174
var shouldDraw = true;170
var shouldDraw = true;175
171
@@ -195,14 +191,29 @@ function image(image, subImgTotal, perRow)195
context.translate(dx, dy);191
context.translate(dx, dy);196
context.rotate(rotation * (Math.PI/180));192
context.rotate(rotation * (Math.PI/180));197
193
198
context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);194
(mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1);199
195
196
context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);197
200
context.restore(); 198
context.restore(); 201
context.globalAlpha = oldAlpha;199
context.globalAlpha = oldAlpha;202
}200
}203
}201
}204
202
205
console.log("Image loaded: " + image.src);203
addAnimationCycle(label, cycle) 204
{205
this.animationCycles[label] = cycle;206
}207
208
get width() 209
{210
return this.g_image.width / this.subImagesPerRow;211
}212
213
get height() 214
{215
return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);216
}206
}217
}207
/*218
/*208
* End File:219
* End File:@@ -349,6 +360,10 @@ class Renderer349
gameObject = null;360
gameObject = null;350
subImage = 0;361
subImage = 0;351
alpha = 1;362
alpha = 1;363
mirrorX = false;364
mirrorY = false;365
366
cycle = null;352
367
353
visible = true;368
visible = true;354
369
@@ -358,6 +373,34 @@ class Renderer358
this.sprite = sprite;373
this.sprite = sprite;359
}374
}360
375
376
update(deltaTime) 377
{378
if(this.cycle) 379
{380
if(this.cycle.cycleSpeed && this.cycle.cycleSpeed == -1) 381
{382
this.subImage = this.cycle.default;383
this.cycle.current = this.cycle.default;384
return;385
}386
this.cycle.timer += deltaTime;387
388
if(this.cycle.timer > this.cycle.cycleSpeed) 389
{390
this.cycle.timer -= this.cycle.cycleSpeed;391
392
this.cycle.cycleArray[this.cycle.current + 1] ?393
this.updateCycleSprite(this.cycle.current + 1) :394
this.updateCycleSprite(0);395
}396
}397
}398
399
updateCycleSprite(index) {400
this.subImage = this.cycle.cycleArray[index];401
this.cycle.current = index;402
}403
361
draw()404
draw()362
{405
{363
if(this.sprite)406
if(this.sprite)@@ -402,9 +445,47 @@ class Renderer402
this.gameObject.transform.size.x, 445
this.gameObject.transform.size.x, 403
this.gameObject.transform.size.y, 446
this.gameObject.transform.size.y, 404
this.gameObject.transform.rotation, 447
this.gameObject.transform.rotation, 448
this.mirrorX,449
this.mirrorY,405
this.alpha450
this.alpha406
);451
);407
}452
}453
454
setAnimation(label, defaultIndex, cycleSpeed) 455
{456
if(this.cycle) 457
{458
if(this.cycle.label == label) 459
{460
this.cycle.defaultIndex = defaultIndex;461
this.cycle.cycleSpeed = cycleSpeed;462
}463
} 464
else465
{466
this.cycle = {467
label: label,468
cycleArray: this.sprite.animationCycles[label], 469
default: defaultIndex,470
current: defaultIndex, 471
cycleSpeed: cycleSpeed,472
timer: 0};473
}474
}475
476
setAnimationCycleSpeed(cycleSpeed) 477
{478
if(this.cycle) 479
{480
this.cycle.cycleSpeed = cycleSpeed;481
}482
}483
484
setSubImage(subImage) 485
{486
this.subImage = subImage;487
this.cycle = null;488
}408
}489
}409
/*490
/*410
* End File:491
* End File:The contents of this binary file are not shown.
@@ -9,12 +9,16 @@ window.onload = function()
9
9
{name:"arrow", url:"images/arrow.png", subImgTotal: 4, perRow: 2},
10
10
{name:"grass_dark", url:"images/grass_dark.png"},
11
11
{name:"grass_light", url:"images/grass_light.png"},
12
{name:"frog_basic", url:"images/character/frog/frog_basic.png", subImgTotal: 8, perRow: 4},
12
{name:"frog_basic", url:"images/character/frog/frog_basic.png", subImgTotal: 4, perRow: 4},
13
13
{name:"frog_tongue_base", url:"images/character/frog/frog_tongue_base.png"},
14
14
{name:"frog_tongue_tip", url:"images/character/frog/frog_tongue_tip.png"}
15
15
]
16
16
);
17
17
18
game.images.frog_basic.addAnimationCycle("walk", [0,1,2,1]);
19
20
console.log(game.images.frog_basic.animationCycles);
21
18
22
this.createMainScene(game);
19
23
game.startGame();
20
24
}
@@ -29,5 +33,6 @@ createMainScene = function(game)
29
33
game.loadScene(scene);
30
34
31
35
scene.addObject(new GrassTiler(50, 50, game.images.arrow, 0));
32
scene.addObject(new Frog(50,50, game.images.frog_basic, 0));
36
frog = new Frog(60,60, game.images.frog_basic, 0);
37
scene.addObject(frog);
33
38
}
@@ -9,12 +9,16 @@ window.onload = function()9
{name:"arrow", url:"images/arrow.png", subImgTotal: 4, perRow: 2},9
{name:"arrow", url:"images/arrow.png", subImgTotal: 4, perRow: 2},10
{name:"grass_dark", url:"images/grass_dark.png"},10
{name:"grass_dark", url:"images/grass_dark.png"},11
{name:"grass_light", url:"images/grass_light.png"},11
{name:"grass_light", url:"images/grass_light.png"},12
{name:"frog_basic", url:"images/character/frog/frog_basic.png", subImgTotal: 8, perRow: 4},12
{name:"frog_basic", url:"images/character/frog/frog_basic.png", subImgTotal: 4, perRow: 4},13
{name:"frog_tongue_base", url:"images/character/frog/frog_tongue_base.png"},13
{name:"frog_tongue_base", url:"images/character/frog/frog_tongue_base.png"},14
{name:"frog_tongue_tip", url:"images/character/frog/frog_tongue_tip.png"}14
{name:"frog_tongue_tip", url:"images/character/frog/frog_tongue_tip.png"}15
]15
]16
);16
);17
17
18
game.images.frog_basic.addAnimationCycle("walk", [0,1,2,1]);19
20
console.log(game.images.frog_basic.animationCycles);21
18
this.createMainScene(game);22
this.createMainScene(game);19
game.startGame();23
game.startGame();20
}24
}@@ -29,5 +33,6 @@ createMainScene = function(game)29
game.loadScene(scene);33
game.loadScene(scene);30
34
31
scene.addObject(new GrassTiler(50, 50, game.images.arrow, 0));35
scene.addObject(new GrassTiler(50, 50, game.images.arrow, 0));32
scene.addObject(new Frog(50,50, game.images.frog_basic, 0));36
frog = new Frog(60,60, game.images.frog_basic, 0);37
scene.addObject(frog);33
}38
}@@ -1,19 +1,18 @@
1
1
class Frog extends GameObject {
2
2
3
facing = {
4
left: [4,5,6],
5
right: [0,1,2]
6
};
7
8
3
speed = 200;
9
4
5
facing =
6
{
7
left: 1,
8
right: 2
9
};
10
10
11
currentFacing = this.facing.right;
11
12
idle = true;
12
13
13
currentIndex = 0;
14
frame = 0;
15
16
constructor(x,y,sprite,layer) {
14
constructor(x,y,sprite,layer)
15
{
17
16
super(x,y,sprite,layer);
18
17
}
19
18
@@ -22,67 +21,45 @@ class Frog extends GameObject {
22
21
super.update(deltaTime);
23
22
this.idle = true;
24
23
25
if(GameInput.isHeld(GameInput.keys.ArrowRight))
24
if(! (GameInput.isHeld(GameInput.keys.ArrowRight) && GameInput.isHeld(GameInput.keys.ArrowLeft)))
26
25
{
27
this.transform.position.x += this.speed * deltaTime;
28
29
if(this.currentFacing != this.facing.right) {
30
this.changeImageDirection();
31
this.currentFacing = this.facing.right;
26
if(GameInput.isHeld(GameInput.keys.ArrowRight))
27
{
28
this.arrowCounter++;
29
this.transform.position.x += this.speed * deltaTime;
30
31
if(this.currentFacing != this.facing.right)
32
{
33
this.renderer.mirrorX = !this.renderer.mirrorX;
34
this.currentFacing = this.facing.right;
35
}
36
37
CookieManager.setCookie("arrow", "right");
38
this.idle = false;
39
this.renderer.setAnimation("walk", 0, .2);
32
40
}
33
41
34
CookieManager.setCookie("arrow", "right");
35
this.idle = false;
36
this.cycleImage(deltaTime);
37
}
38
39
if(GameInput.isHeld(GameInput.keys.ArrowLeft))
40
{
41
this.transform.position.x -= this.speed * deltaTime;
42
43
if(this.currentFacing != this.facing.left) {
44
this.changeImageDirection();
45
this.currentFacing = this.facing.left;
42
if(GameInput.isHeld(GameInput.keys.ArrowLeft))
43
{
44
this.arrowCounter++;
45
this.transform.position.x -= this.speed * deltaTime;
46
47
if(this.currentFacing != this.facing.left)
48
{
49
50
this.renderer.mirrorX = !this.renderer.mirrorX;
51
this.currentFacing = this.facing.left;
52
}
53
54
CookieManager.setCookie("arrow", "left")
55
this.idle = false;
56
this.renderer.setAnimation("walk", 0, .2);
46
57
}
47
48
CookieManager.setCookie("arrow", "left")
49
this.idle = false;
50
this.cycleImage(deltaTime);
51
58
}
52
59
53
if(this.idle) {
54
this.currentIndex = 0;
55
this.resolveToIdleImage();
56
}
57
}
58
59
changeImageDirection() {
60
if(this.renderer.subImage > 3) {
61
this.renderer.subImage -= 4;
62
} else {
63
this.renderer.subImage += 4;
64
}
65
}
66
67
resolveToIdleImage() {
68
if(this.renderer.subImage > 3) {
69
if(this.renderer.subImage != 4) {
70
this.renderer.subImage = 4;
71
}
72
} else {
73
if(this.renderer.subImage != 0) {
74
this.renderer.subImage = 0;
75
}
76
}
77
}
78
79
cycleImage(deltaTime) {
80
this.frame+= deltaTime;
81
82
if(this.frame > .05) {
83
this.frame = 0;
84
this.currentIndex++;
85
this.renderer.subImage = this.currentFacing[this.currentIndex % this.currentFacing.length];
60
if(this.idle)
61
{
62
this.renderer.setAnimationCycleSpeed(-1);
86
63
}
87
64
}
88
65
}
@@ -1,19 +1,18 @@1
class Frog extends GameObject {1
class Frog extends GameObject {2
2
3
facing = {4
left: [4,5,6],5
right: [0,1,2]6
};7
8
speed = 200;3
speed = 200;9
4
5
facing = 6
{ 7
left: 1,8
right: 29
};10
10
currentFacing = this.facing.right;11
currentFacing = this.facing.right;11
idle = true;12
idle = true;12
13
13
currentIndex = 0;14
constructor(x,y,sprite,layer) 14
frame = 0;15
{15
16
constructor(x,y,sprite,layer) {17
super(x,y,sprite,layer);16
super(x,y,sprite,layer);18
}17
}19
18
@@ -22,67 +21,45 @@ class Frog extends GameObject {22
super.update(deltaTime);21
super.update(deltaTime);23
this.idle = true;22
this.idle = true;24
23
25
if(GameInput.isHeld(GameInput.keys.ArrowRight))24
if(! (GameInput.isHeld(GameInput.keys.ArrowRight) && GameInput.isHeld(GameInput.keys.ArrowLeft))) 26
{25
{27
this.transform.position.x += this.speed * deltaTime;26
if(GameInput.isHeld(GameInput.keys.ArrowRight))28
27
{29
if(this.currentFacing != this.facing.right) {28
this.arrowCounter++;30
this.changeImageDirection();29
this.transform.position.x += this.speed * deltaTime;31
this.currentFacing = this.facing.right;30
31
if(this.currentFacing != this.facing.right) 32
{33
this.renderer.mirrorX = !this.renderer.mirrorX;34
this.currentFacing = this.facing.right;35
}36
37
CookieManager.setCookie("arrow", "right");38
this.idle = false;39
this.renderer.setAnimation("walk", 0, .2);32
}40
}33
41
34
CookieManager.setCookie("arrow", "right");42
if(GameInput.isHeld(GameInput.keys.ArrowLeft))35
this.idle = false;43
{36
this.cycleImage(deltaTime);44
this.arrowCounter++;37
}45
this.transform.position.x -= this.speed * deltaTime;38
46
39
if(GameInput.isHeld(GameInput.keys.ArrowLeft))47
if(this.currentFacing != this.facing.left) 40
{48
{41
this.transform.position.x -= this.speed * deltaTime;49
42
50
this.renderer.mirrorX = !this.renderer.mirrorX;43
if(this.currentFacing != this.facing.left) {51
this.currentFacing = this.facing.left;44
this.changeImageDirection();52
}45
this.currentFacing = this.facing.left;53
54
CookieManager.setCookie("arrow", "left")55
this.idle = false;56
this.renderer.setAnimation("walk", 0, .2);46
}57
}47
48
CookieManager.setCookie("arrow", "left")49
this.idle = false;50
this.cycleImage(deltaTime);51
}58
}52
59
53
if(this.idle) {60
if(this.idle) 54
this.currentIndex = 0;61
{55
this.resolveToIdleImage();62
this.renderer.setAnimationCycleSpeed(-1);56
}57
}58
59
changeImageDirection() {60
if(this.renderer.subImage > 3) {61
this.renderer.subImage -= 4;62
} else {63
this.renderer.subImage += 4;64
}65
}66
67
resolveToIdleImage() {68
if(this.renderer.subImage > 3) {69
if(this.renderer.subImage != 4) {70
this.renderer.subImage = 4;71
}72
} else {73
if(this.renderer.subImage != 0) {74
this.renderer.subImage = 0;75
}76
}77
}78
79
cycleImage(deltaTime) {80
this.frame+= deltaTime;81
82
if(this.frame > .05) {83
this.frame = 0;84
this.currentIndex++;85
this.renderer.subImage = this.currentFacing[this.currentIndex % this.currentFacing.length];86
}63
}87
}64
}88
}65
}@@ -19,7 +19,7 @@ class GrassTiler extends GameObject
19
19
for(let y = 0; y < height; y += this.ground_light.height)
20
20
{
21
21
count++;
22
((count) %2 == 1? this.ground_dark : this.ground_light).draw(this.scene, 0, 0, this.ground_dark.width, this.ground_dark.height, x - 32, y, this.ground_dark.width, this.ground_light.height, 0, 1)
22
((count) %2 == 1? this.ground_dark : this.ground_light).draw(this.scene, 0, 0, this.ground_dark.width, this.ground_dark.height, x - 32, y, this.ground_dark.width, this.ground_light.height, 0, 0, 0, 1)
23
23
}
24
24
25
25
count = x/64 % 2;
@@ -19,7 +19,7 @@ class GrassTiler extends GameObject19
for(let y = 0; y < height; y += this.ground_light.height)19
for(let y = 0; y < height; y += this.ground_light.height)20
{20
{21
count++;21
count++;22
((count) %2 == 1? this.ground_dark : this.ground_light).draw(this.scene, 0, 0, this.ground_dark.width, this.ground_dark.height, x - 32, y, this.ground_dark.width, this.ground_light.height, 0, 1)22
((count) %2 == 1? this.ground_dark : this.ground_light).draw(this.scene, 0, 0, this.ground_dark.width, this.ground_dark.height, x - 32, y, this.ground_dark.width, this.ground_light.height, 0, 0, 0, 1)23
}23
}24
24
25
count = x/64 % 2;25
count = x/64 % 2;