-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc_app_sprite.js
94 lines (92 loc) · 2.63 KB
/
sc_app_sprite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* SC_sprite.js
* Author : Jean-Ferdy Susini
* Created : 15/12/2014 0:06 AM
* version : 0.1 alpha
* implantation : 0.1
* Copyleft 2014-2024.
*/
function SC_sprite(p){
const param= SC.$(p);
var errors= "";
if(undefined==param.sIA){
errors+= "SC_sprite : sIA parameter must be provided (images) !\n";
}
if(undefined==param.sIM){
errors+= "SC_sprite : sIM parameter must be provided (numbers of tiles"
+"in x and y for each images) !\n";
}
if(undefined==param.sIO){
errors+= "SC_sprite : sIO parameter must be provided (tiles"
+"offsets in x and y for each images) !\n";
}
if(errors!=""){
throw new Error(errors);
}
this.sIA= param.sIA;
this.sIM= param.sIM;
this.sIO= param.sIO;
this.sII= param._('sII', 0);
this.sFIx= param._('sFIx', 0);
this.sFIy= param._('sFIy', 0);
// À modifier utilise workspace comme globale
this.pos= new Point2D(param._('x', Math.random()*workspace.width)
, param._('y', Math.random()*workspace.height));
this.setSpriteImage(this.sII);
this.worldDim= param._("wd");
//this.r= new Point2D(this.x, this.y);
}
(function(){
const proto= SC_sprite.prototype;
proto.setSpriteImageOffsets= function(){
this.cSIOx= this.sIO[this.sII].x;
if(undefined==this.cSIOx){
this.cSIOx=0;
}
this.cSIOy= this.sIO[this.sII].y;
if(undefined==this.cSIOy){
this.cSIOy= 0;
}
};
proto.setSpriteWidthAndHeight= function(){
this.sw= this.img.width/this.sIM[this.sII].x;
this.sh= this.img.height/this.sIM[this.sII].y;
};
proto.setSpritePosInX= function(x){
this.sFIx= (x)%(this.sIM[this.sII].x);
};
proto.nextSpriteInX= function(){
this.setSpritePosInX(this.sFIx+1);
};
proto.setSpritePosInY= function(y){
this.sFIy= (y)%(this.sIM[this.sII].y);
if(this.sFIy<0){
console.warn("this.sFIy", this.sFIy, y);
debugger;
}
};
proto.nextSpriteInY= function(){
this.setSpritePosInY(this.sFIy+1);
};
proto.setSpriteImage= function(phase){
this.sII= (phase)%(this.sIA.length);
this.img= this.sIA[this.sII];
this.setSpriteWidthAndHeight();
this.setSpriteImageOffsets();
};
proto.nextSpriteImage= function(){
this.setSpriteImage(this.sII+1);
};
proto.draw= function(ctx, view){
const { x, y, w: cw, h: ch }= view;
const { x: px, y: py }= this.pos;
const theCtx= ctx.save();
ctx.translate(px-x, py-y);
const sw= this.sw;
const sh= this.sh;
ctx.drawImage(this.img
, this.sFIx*sw, this.sFIy*sh, sw, sh
, -sw/2+this.cSIOx, -sh/2+this.cSIOy, sw, sh);
ctx.restore(theCtx);
};
})();