OSDN Git Service

当たり判定の大規模な修正。
[h58pcdgame/GameScriptCoreLibrary.git] / www / corelib / classes / SlopeBlockClass.js
1 var SlopeBlockClass = function(ownerStage, args)
2 {
3         SlopeBlockClass.base.apply(this, arguments);
4         
5         var colLH = args[1], colRH = args[2];
6         
7         this.collisionLeftHeight = colLH;               //\8d\8fã\82©\82ç\95Ó\82è\94»\92è\97Ì\88æ\82Ü\82Å\82Ì\8d\82\82³
8         this.collisionRightHeight = colRH;              //\89E\8fã\82©\82ç\95Ó\82è\94»\92è\97Ì\88æ\82Ü\82Å\82Ì\8d\82\82³
9         
10         //\83f\83o\83b\83O\8fî\95ñ\82ð\89æ\96Ê\82É\95\\8e¦
11         this.enableDebugMode = false;
12         
13         //\83f\83o\83b\83O\8fî\95ñ
14         this.x1 = 0;
15         this.x2 = 0;
16         this.y2 = 0;
17         
18 }.extend(BlockClass, {
19         isOverlappedWith: function(x1, y1, x2, y2){
20                 // 1 - - +
21                 // |     |
22                 // |     |
23                 // + - - 2
24                 //\83I\83u\83W\83F\83N\83g\96{\91Ì\95\94\95ª\82É\8fd\82È\82é\82©\82Ç\82¤\82©\82ð\92²\82×\82é
25                 //\82±\82Ì\8aÖ\90\94\82ªfalse\82ð\95Ô\82µ\82½\8e\9e\82Ì\88ø\90\94\82Ì\94Í\88Í\82Ö\82Í\83I\83u\83W\83F\83N\83g\82ª\88Ú\93®\82Å\82«\82é\81B
26                 //true\82©\82ÂisPhantom==false\82Ì\8e\9e\82Í\8fÕ\93Ë\82·\82é\81B
27                 
28                 //\83f\83o\83b\83O\8fî\95ñ
29                 this.x1 = x1;
30                 this.y2 = y2;
31                 this.x2 = x2;
32                 
33                 if(this.collisionLeftHeight != this.collisionRightHeight){
34                         return this.crossLineJudge(x1, y2 - 1, x2, y2 - 1,
35                                                                                 this.origin.x, this.origin.y + this.collisionLeftHeight,
36                                                                                 this.origin.x + this.size.x - 1, this.origin.y + this.collisionRightHeight);
37                 }
38                 //\83`\83F\83b\83N\82·\82é\93ñ\82Â\82Ì\90ü\95ª\82ª\95½\8ds\82¾\82Æ\93\96\82½\82è\94»\92è\82É\98R\82ê\82é\82Ì\82Å\81A\93\96\82½\82è\94»\92è\82ª\90\85\95½\82È\8fê\8d\87\82Í\91¼\82Ì\8e®\82ð\97\98\97p\82·\82é\81B
39                 if(this.collisionLeftHeight >= this.size.y){
40                         return false;
41                 }
42                 return (x2 > (this.origin.x + this.collisionMargin.left)) &&
43                         (x1 <= (this.origin.x + this.size.x - this.collisionMargin.right)) &&
44                         (y2 > (this.origin.y + this.collisionMargin.top + this.collisionLeftHeight)) &&
45                         (y1 <= (this.origin.y + this.size.y - this.collisionMargin.bottom));
46         },
47         //\8dÀ\95W p1,p2 \82ð\8c\8b\82Ô\90ü\95ª\82Æ\8dÀ\95W q1,q2 \82ð\8c\8b\82Ô\90ü\95ª\82ª\8cð\8d·\82µ\82Ä\82¢\82é\82©\82ð\92²\82×\82é
48         //\82½\82¾\82µ\81A\90ü\95ª\82ª\8fd\82È\82Á\82Ä\82¢\82é\8fê\8d\87(3\93_,4\93_\82ª\88ê\92¼\90ü\8fã\82É\82 \82é)\81A\81u\8cð\8d·\82µ\82Ä\82¢\82é\81v\81A\82Æ\94»\92è\82µ\82Ü\82·\81B
49         crossLineJudge: function(p1x, p1y, p2x, p2y, q1x, q1y, q2x, q2y){
50                 if(p1x >= p2x)
51                 {
52                         if((p1x < q1x && p1x < q2x) || (p2x > q1x && p2x > q2x)){
53                                 return false;
54                         }
55                 } else{
56                         if((p2x < q1x && p2x < q2x) || (p1x > q1x && p1x > q2x)){
57                                 return false;
58                         }
59                 }
60                 if(p1y >= p2y)
61                 {
62                         if((p1y < q1y && p1y < q2y) || (p2y > q1y && p2y > q2y)){
63                                 return false;
64                         }
65                 } else{
66                         if((p2y < q1y && p2y < q2y) || (p1y > q1y && p1y > q2y)){
67                                 return false;
68                         }
69                 }
70                 return !((((p1x - p2x)*(q1y-p1y)+(p1y-p2y)*(p1x-q1x))*((p1x-p2x)*(q2y-p1y)+(p1y-p2y)*(p1x-q2x))>0) ||
71                                  (((q1x - q2x)*(p1y-q1y)+(q1y-q2y)*(q1x-p1x))*((q1x-q2x)*(p2y-q1y)+(q1y-q2y)*(q1x-p2x))>0));
72                 //\8eQ\8dl http://www5d.biglobe.ne.jp/~tomoya03/shtml/algorithm/IntersectionEX.htm
73         },
74         draw : function(x, y){
75                 SlopeBlockClass.base.prototype.draw.apply(this, arguments);
76                 
77                 if(this.enableDebugMode)
78                 {
79                         var xz = this.origin.x - x;
80                         this.ownerStage.mainContext.save();
81                         this.ownerStage.mainContext.beginPath();
82                         this.ownerStage.mainContext.strokeStyle='#0000ff';
83                         this.ownerStage.mainContext.moveTo(x,y + this.collisionLeftHeight);
84                         this.ownerStage.mainContext.lineTo(x + this.size.x + 1,y + this.collisionRightHeight);
85                         this.ownerStage.mainContext.stroke();
86                         this.ownerStage.mainContext.beginPath();
87                         this.ownerStage.mainContext.strokeStyle='#ff0000';
88                         this.ownerStage.mainContext.moveTo(this.x1 - xz,this.y2);
89                         this.ownerStage.mainContext.lineTo(this.x2 - xz,this.y2);
90                         this.ownerStage.mainContext.stroke();
91                         this.ownerStage.mainContext.restore();
92                 }
93         },
94         debugDraw: function(x, y){
95                 var xz = this.origin.x - x;
96                 
97                 if(this.collisionLeftHeight < this.size.y && this.collisionRightHeight < this.size.y){
98                         this.ownerStage.collisionMapContext.beginPath();
99                         this.ownerStage.collisionMapContext.moveTo(x, y + this.collisionLeftHeight);
100                         this.ownerStage.collisionMapContext.lineTo(x + this.size.x, y + this.collisionRightHeight);
101                         this.ownerStage.collisionMapContext.lineTo(x + this.size.x, y + this.size.y);
102                         this.ownerStage.collisionMapContext.lineTo(x, y + this.size.y);
103                         this.ownerStage.collisionMapContext.closePath();
104                         this.ownerStage.collisionMapContext.stroke();
105                 } else{
106                         this.ownerStage.collisionMapContext.beginPath();
107                         this.ownerStage.collisionMapContext.moveTo(x, y + this.collisionLeftHeight);
108                         this.ownerStage.collisionMapContext.lineTo(x + this.size.x, y + this.collisionRightHeight);
109                         this.ownerStage.collisionMapContext.stroke();
110                 }
111         },
112 });
113 //\82±\82Ì\83N\83\89\83X\82Ì\83C\83\93\83X\83^\83\93\83X\82Ìtick\82ð\8cÄ\82Ñ\8fo\82³\82È\82¢\82æ\82¤\82É\90Ý\92è\81i\8d\82\91¬\89»\81j
114 SlopeBlockClass.tick = function(){return false;};