-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentity.js
137 lines (115 loc) · 2.94 KB
/
entity.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
/* exported entity, drawnEntity */
/* global offsetPick, mouse, dragDrop, mergeObject,Animation,drawAt,engine */
"use strict";
var entity = function (settings) {
// default values
var ini = {
type:'entity',
global:true // the engine will tick it directly
};
// merge settings with defaults
var entity = mergeObject(settings,ini);
entity.update = function() {};
entity.destroy = function() {
if(this.global) {
engine.entities.erase(this);
engine.objects.erase(this);
log('entity destroyed');
}
};
entity.toString = function() {
return (this.type+'#'+this.GUID+': '+this.position);
};
entity.GUID = getGuid();
if(entity.global) {
engine.objects.push(entity);
engine.entities.push(entity);
}
return entity;
};
var drawnEntity = function (settings) {
// default values
var ini = {
type:'drawnEntity',
layerID : 0,
layerName: null,
position : new Point(0,0),
hotspot : new Point(0,0),
size : new Point(0,0),
angle : 0,
frames:[]
};
// merge settings with defaults
settings = mergeObject(settings,ini);
// inherit from entity
var drawnEntity = entity(settings);
// every displayed object is drawn via an animation
if (drawnEntity.frames) {
drawnEntity.animation = new Animation(drawnEntity,drawnEntity.frames);
}
if (drawnEntity.layerID !== -1) {
// register the entity in its proper layer
if (drawnEntity.layerName !== null) {
engine.layerName[drawnEntity.layerName].entities.push(drawnEntity);
drawnEntity.layer = engine.layerName[drawnEntity.layerName];
} else if (drawnEntity.layerID !== null) {
engine.layers[drawnEntity.layerID].entities.push(drawnEntity);
drawnEntity.layer = engine.layers[drawnEntity.layerID];
} else {
throw 'no layer ID!';
}
}
drawnEntity.update = function(dt) {
this.animation.update(dt);
};
drawnEntity.draw = function(ctx) {
var self = this;
drawAt(ctx,this.position,this.angle,function(ctx) {
// TODO: add hotspot offset calculation
self.animation.draw(ctx,0,0,self.size.x,self.size.y);
});
};
var superDestroy = drawnEntity.destroy;
drawnEntity.destroy = function() {
superDestroy.call(this);
this.layer.entities.erase(this);
};
drawnEntity.pick = function() {log('picked '+this);};
drawnEntity.unPick = function() {};
return drawnEntity;
};
var dummy = function (settings) {
// default values
var ini = {
type:'drawnEntity',
layerID : 0,
layerName: null,
position : new Point(0,0),
hotspot : new Point(0,0),
size : new Point(0,0),
angle : 0,
frames:[]
};
// merge settings with defaults
settings = mergeObject(settings,ini);
// inherit from entity
var dummy = drawnEntity(settings);
// calculate offset to the mouse
offsetPick.call(dummy);
dummy.update = function () {
dragDrop.call(this);
if (!mouse.left) {
this.destroy();
}
};
return dummy;
};
// simple autoincrement GUID
var getGuid = (function() {
var guid = -1;
var getGuid = function() {
guid += 1 ;
return guid;
};
return getGuid;
})();