-
Notifications
You must be signed in to change notification settings - Fork 1
/
element_factory.js
161 lines (159 loc) · 5.55 KB
/
element_factory.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
module.exports = ElementFactory;
require("./lives.js");
require("./score.js");
require("./aceleration.js");
require("./velocity.js");
require("./poligon.js");
require("./retangle.js");
require("./quad.js");
require("./border.js");
require("./arc.js");
require("./levels.js");
var decamelize = require("decamelize");
var Velocity = require("./velocity.js");
var Point = require("./point.js");
function ElementFactory() { }
ElementFactory.prototype = {
createElement: function(type_class, params) {
var file = "./" + decamelize(type_class) + ".js";
var Class = require(file);
var tmp_obj = new Class(params);
tmp_obj._class = type_class;
tmp_obj.clone = function() {
var clone = this._element_factory.screen.createElement(this._class);
for(var attr in this) {
if(this[attr].clone != null) clone[attr] = this[attr].clone();
else clone[attr] = this[attr];
}
return clone;
};
tmp_obj.__defineGetter__("height", function(){return this.retangle_definition().height});
tmp_obj.__defineGetter__("width", function(){return this.retangle_definition().width});
tmp_obj.stoped = false;
tmp_obj.stop = function(){
this.stopped = true;
};
tmp_obj.resume = function(){
this.stopped = false;
};
tmp_obj._element_factory = this;
tmp_obj._orig_draw = tmp_obj.draw;
var screen = tmp_obj.screen = this.screen;
var element = tmp_obj;
if(tmp_obj.colision_area == null) tmp_obj.colision_area = tmp_obj.retangle_definition;
if(tmp_obj._orig_draw != null) {
tmp_obj.draw = function() {
this._element_factory.default_before_draw_func(element);
this._orig_draw(screen);
this._element_factory.default_after_draw_func(element);
};
}
tmp_obj.__defineGetter__("center", function() {
return new Point(this.center_x(), this.center_y());
});
tmp_obj.bounce = function(element) {
var eret = element.retangle_definition();
var _this = this;
setTimeout(function(){_this.orig_velocity = _this.velocity}, 10);
if(this.center_x() <= eret.Ax && this.velocity.x > 0)
this.velocity.x *= -1;
if(this.center_x() >= eret.Bx && this.velocity.x < 0)
this.velocity.x *= -1;
if(this.center_y() <= eret.Ay && this.velocity.y > 0)
this.velocity.y *= -1;
if(this.center_y() >= eret.Cy && this.velocity.y < 0)
this.velocity.y *= -1;
this.velocity.ang += (Math.random() - 0.5) / 10;
},
tmp_obj.destroy = function() {
if(this.before_destroy != null) this.before_destroy();
this._element_factory.screen.removeElement(this);
},
tmp_obj.center_x = function() {
var ret = this.retangle_definition();
return(ret.Ax + ((ret.Bx - ret.Ax) / 2))
};
tmp_obj.center_y = function() {
var ret = this.retangle_definition();
return(ret.Ay + ((ret.Cy - ret.Ay) / 2))
};
tmp_obj.velocity = new Velocity();
tmp_obj.velocity.element = tmp_obj;
if(tmp_obj.solid == null) tmp_obj.solid = true;
tmp_obj.move = function() {
if(this.stopped) return;
if((this.velocity.x == null || this.velocity.x == 0)
&& (this.velocity.y == null || this.velocity.y == 0))
return;
if(this.flutuate != null && !this.flutuate && this._element_factory.screen.gravity != null) {
//if(this.type != "ball") console.log("usando gravidade!");
var current_time = (new Date()).getTime();
if(this.last_fall == null) this.last_fall = current_time;
var passed = current_time - this.last_fall;
if(passed < 1) return;
if(passed >= 100) {
var to_add = this._element_factory.screen.gravity.get_velocity_alteration_in_ms(passed);
this.velocity.add(to_add);
this.last_fall = current_time;
}
}
//this.velocity.dump();
this.x += this.velocity.x;
this.y += this.velocity.y;
if(tmp_obj.on_move != null) tmp_obj.on_move();
};
tmp_obj.elementsColiding = function(element) {
};
tmp_obj.on_colide_with = function(type, sub) {
if(this.types_to_colide == null) this.types_to_colide = {};
this.types_to_colide[type] = sub;
};
tmp_obj.on_colide_with_multiple = function(type, sub) {
if(this.types_to_multiple_colide == null) this.types_to_multiple_colide = {};
this.types_to_multiple_colide[type] = sub;
};
tmp_obj.can_colide_with = function(element, reverse) {
if(reverse == null) {
if(!element.can_colide_with(this, false))return false;
}
if(this.do_not_colide_with != null) {
var cant = this.do_not_colide_with;
for(var i = 0; i < cant.length; i++) {
if(element.type == cant[i]) return false;
}
}
return true;
};
return tmp_obj;
},
default_before_draw_func: function(element) {
var screen = this.screen;
screen.ctx.save();
screen.ctx.translate(element.x,element.y);
if(element.solid && element.color != null) screen.ctx.fillStyle = element.color;
else if(element.color != null) screen.ctx.strokeStyle = element.color;
screen.ctx.beginPath();
},
default_after_draw_func: function(element) {
var screen = this.screen;
if(element.solid) screen.ctx.fill();
else screen.ctx.stroke();
screen.ctx.restore();
if(element.draw_center_point) {
this.default_before_draw_func(element.center);
element.center.draw(screen);
this.default_after_draw_func(element.center);
}
if(element.draw_colision_area) {
this.default_before_draw_func(element.retangle_definition());
element.retangle_definition().draw(screen);
this.default_after_draw_func(element.retangle_definition());
}
if(element.draw_velocity) {
this.default_before_draw_func(element.velocity);
element.velocity.draw(screen);
this.default_after_draw_func(element.velocity);
}
},
//draw_retangle: false,
};