-
Notifications
You must be signed in to change notification settings - Fork 2
/
generic-cc.js
139 lines (117 loc) · 4.28 KB
/
generic-cc.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
/**
* Generic Command Controller
* @desc Common attributes and methods for client and server command
* controllers
* @resp Reads and writes the models
* @resp Receives the game variables from the command controller
* @resp Updates the model
* @resp Calls the graphic controller back
*/
if (typeof CLIENT_SIDE === 'undefined') {
var app = require('./public/models/model.app.js').app,
Ball = require('./public/models/model.ball.js').Ball
Cap = require('./public/models/model.cap.js').Cap,
field = require('./public/models/model.field.js').field,
PreviewBall = require('./public/models/model.ball.js').PreviewBall,
Team = require('./public/models/model.team.js').Team,
utils = require('./public/utils.js').utils;
}
function CommandController () {
this.genericInit = function () {
// object creation needed by both client and server
app.caps[0] = new Cap(1, 50, 50, Team.LOCAL);
app.caps[1] = new Cap(2, 100, 100, Team.LOCAL);
app.caps[2] = new Cap(3, 150, 150, Team.VISITOR);
app.caps[3] = new Cap(4, 200, 200, Team.VISITOR);
app.ball = new Ball(200, 200);
};
/**
* The reverse of setStatus
* Used for client-server communication
* getStatus is actually only used by the client; could be moved
* to client-cc.js
* @return {JSON} Status of the game variables
*/
this.getStatus = function () {
return {
ball: app.ball.getStatus(),
caps: app.caps.map(function (c) { return c.getStatus() })
}
}
/**
* The reverse of to getStatus
* Used for client-server communication
* setStatus is actually only used by the server; could be moved
* to server-cc.js
* @param st {JSON} Status of the game variables
* @post Writes the model with the variables in st
*/
this.setStatus = function (st) {
// ball
app.ball.setPosition(st.ball.x, st.ball.y);
app.ball.poss = st.ball.poss ? app.getCapById(st.ball.poss) : null;
// caps
st.caps.forEach(function (c) {
app.getCapById(c.id).setPosition(c.x, c.y);
});
};
// unstun all the caps, every turn. caps are stunned only 1 turn
this.unstun = function () {
app.unstun();
};
this.move = function (capId, x, y) {
var cap = app.getCapById(capId);
// if this cap is covered by another cap, we should also
// move it. Done before moving the current cap because
// at this point we have origin and end of movement
if (cap.coveredBy instanceof Cap) {
dist = utils.getEuclideanDistance(cap.x, cap.y, x, y);
var capD = cap.coveredBy,
x_mov = x - cap.x,
y_mov = y - cap.y;
capD.moveIfCovering(x_mov, y_mov);
// if the separation between both caps now is bigger
// than defemders's D area the cover is broken
separation = utils.getEuclideanDistance(x, y, capD.x, capD.y);
if (separation > capD.getDefenseRange()) {
capD.covering = null;
cap.coveredBy = null;
}
}
/*
* TODO: checking whether can put cap on coords
* because the client can't be trusted
*/
cap.setPosition(x, y);
// set possession
if (cap.isCapOverTheBall()) {
app.possession.give(cap);
}
};
this.tackle = function(capId, x, y) {
app.tackle(capId, x, y);
};
this.cover = function(capId, x, y) {
app.cover(capId, x, y);
};
this.dribbling = function(capId, x, y) {
app.dribbling(capId, x, y);
};
/**
* @pre ball.poss is the cap passing
*/
this.pass = function (x, y) {
distanceInRedZone = app.pass.getDistanceInRedZone(x, y);
// pass to green zone
if (distanceInRedZone <= 0) {
console.log("pass to green zone");
app.pass.toGreenZone(x, y);
// pass to red zone
} else {
console.log("pass to red zone");
app.pass.toRedZone(x, y);
}
app.pass.checkPossession();
};
};
if (typeof CLIENT_SIDE === 'undefined') exports.CommandController = CommandController;