forked from cvan/tanx-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.js
126 lines (105 loc) · 4.82 KB
/
link.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
var tmpVec = new pc.Vec3();
var tmpVec2 = new pc.Vec3();
var tmpVec3 = new pc.Vec3();
pc.script.create('link', function (context) {
var Link = function (entity) {
this.entity = entity;
this.entity.link = this.link.bind(this);
this.link = null;
this.vec = new pc.Vec2();
this.angle = 0;
this.lastAngle = 0;
this.lastSend = 0;
this.mPos = [ 0, 0 ];
};
Link.prototype = {
initialize: function () {
context.mouse.on(pc.input.EVENT_MOUSEMOVE, this.onMouseMove, this);
this.client = context.root.getChildren()[0].script.client;
context.root.getChildren()[0].script.culling.setCamera(this.entity.camera);
if (! ('ontouchstart' in document.documentElement)) {
this.target = context.root.findByName('target');
this.mouse = {
move: false,
x: 0,
y: 0
};
} else {
context.root.findByName('target').destroy();
}
},
update: function (dt) {
if (this.link) {
var target = this.link;
// choose focus target
if (this.link.script.tank.dead) {
if (this.target)
this.target.enabled = false;
if (this.link.script.tank.killer && this.link.script.tank.killer.script && ! this.link.script.tank.killer.script.tank.dead) {
// focus on killer
target = this.link.script.tank.killer;
} else {
target = null;
}
} else {
if (this.target)
this.target.enabled = true;
}
if (target) {
// rotate vector
var rot = [ 0, 0 ];
if (! this.link.script.tank.dead) {
rot = this.mPos.slice(0);
var t = rot[0] * Math.sin(Math.PI * 0.75) - rot[1] * Math.cos(Math.PI * 0.75);
rot[1] = rot[1] * Math.sin(Math.PI * 0.75) + rot[0] * Math.cos(Math.PI * 0.75);
rot[0] = t;
}
tmpVec.set(
target.getPosition().x + 8.5 + (rot[0] / (context.graphicsDevice.width / 2) * 4),
12,
target.getPosition().z + 8.5 + (rot[1] / (context.graphicsDevice.height / 2) * 4)
);
} else {
tmpVec.set(24 + 8.5, 12, 24 + 8.5);
}
this.entity.setPosition(tmpVec.lerp(this.entity.getPosition(), tmpVec, 0.1));
if (this.mouse && this.mouse.move) {
// camera offset
this.mPos[0] = this.mouse.x - context.graphicsDevice.width / 2;
this.mPos[1] = this.mouse.y - context.graphicsDevice.height / 2;
// cursor
var point = this.entity.camera.screenToWorld(this.mouse.x, this.mouse.y, 2);
if (this.target)
this.target.setPosition(point);
// targeting
if (! this.link.script.tank.dead) {
var camPos = this.entity.getPosition();
var mouseWPos = this.entity.camera.screenToWorld(this.mouse.x, this.mouse.y, 1);
var mouseDir = tmpVec.set(0, 0, 0).sub2(mouseWPos, camPos).normalize();
var planeNormal = tmpVec3.set(0, 1, 0);
var rayPlaneDot = planeNormal.dot(mouseDir);
var pointPlaneDist = (planeNormal.dot(camPos) - 0) / rayPlaneDot;
var pickedPos = mouseDir.scale(-pointPlaneDist).add(camPos);
pickedPos.sub(this.link.getPosition()).normalize();
this.angle = Math.floor(Math.atan2(pickedPos.x, pickedPos.z) / (Math.PI / 180));
this.link.targeting(this.angle);
}
}
}
if (Date.now() - this.lastSend > 100 && this.angle !== this.lastAngle) {
this.lastSend = Date.now();
this.lastAngle = this.angle;
this.client.socket.send('target', this.angle);
}
},
onMouseMove: function(evt) {
this.mouse.x = evt.x;
this.mouse.y = evt.y;
this.mouse.move = true;
},
link: function(tank) {
this.link = tank;
}
};
return Link;
});