-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
176 lines (152 loc) · 5.47 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var gamejs = require('gamejs');
var objects = require('gamejs/utils/objects');
var BuildMenu = require('./view/buildmenu').BuildMenu;
var ShipMenu = require('./view/shipmenu').ShipMenu;
var MainMenu = require('./view/mainmenu').MainMenu;
var ServerWorld = require('./model').World;
var ViewWorld = require('./view').World;
var UNIT_TYPES = require('./model/unit').TYPES;
var statlogger = require('./view/statlogger');
var Hud = require('./view/hud').Hud;
var touch = require('./touch');
var screen = [
832,
480
];
var cellSize = [32, 32];
var worldSize = [
Math.floor(screen[0] / cellSize[0]),
Math.floor(screen[1] / cellSize[1])
];
var hashRounds = parseInt(document.location.hash.substring(1), 10);
// disable normal browser mouse select
function browserFixes() {
// no text select on drag
document.body.style.webkitUserSelect = 'none';
// non right clickery
//document.body.oncontextmenu = function() { return false; };
// FIXME IEBUG CHROMEBUG
// very ugly hack until they fixes some of their
// audio bugs we disable audio completely
if (navigator.userAgent.indexOf('Chrome') > -1 || navigator.userAgent.indexOf('MSIE') > -1 ||
navigator.userAgent.indexOf('Safari') > -1) {
gamejs.mixer.Sound = function() {
return {
play: function() {}
};
};
}
}
var imgsPreload = [];
UNIT_TYPES.forEach(function(type) {
imgsPreload.push('images/client/' + type + '.png');
});
['year', 'population', 'gold', 'harmony1', 'harmony2', 'harmony3', 'map02'].forEach(function(t) {
imgsPreload.push('images/client/' + t + '.png');
});
['impactfall', 'impactwood01', 'sell_buy', 'rain_2', 'thunder_1_far', 'move_fail', 'move_okay'].forEach(function(s) {
imgsPreload.push('sounds/client/' + s + '.ogg');
});
gamejs.preload(imgsPreload);
gamejs.ready(main);
function main() {
browserFixes();
var cellCursor = new gamejs.Rect([0,0], cellSize);
function handleEvent(event) {
if (event.type === gamejs.event.MOUSE_UP) {
var cell = viewWorld.eventPosToCell(event.pos);
if (viewWorld.isIsland(cell)) {
buildMenu.show(cell, event.pos);
}
if (viewWorld.isOwnShip(cell)) {
shipMenu.show(cell);
}
} else if (event.type === gamejs.event.MOUSE_MOTION) {
cellCursor.left = event.pos[0] - (event.pos[0] % cellSize[0]);
cellCursor.top = event.pos[1] - (event.pos[1] % cellSize[1]);
} else if (event.type === gamejs.event.KEY_UP) {
if (event.key === gamejs.event.K_ESC) {
mainMenu.show();
}
};
};
var lastIsVisible = 0;
function tick(msDuration) {
if (shipMenu.isVisible()) {
shipMenu.update(msDuration);
} else if (buildMenu.isVisible()) {
lastIsVisible = Date.now();
} else if (mainMenu.isVisible()) {
lastIsVisible = Date.now();
return;
} else if (Date.now() - lastIsVisible < 400) {
// danger stupid, throwing away events
gamejs.event.get().forEach(function() {});
} else {
gamejs.event.get().forEach(handleEvent);
};
// TODO verify orders serverside
// in an intermediat 'Controller' class, that will
// be the actions on serverside.
// TODO
// money dealing on server-side & clientside
buildMenu.getOrders().forEach(function(order) {
gamejs.debug('build order, sending ', JSON.stringify(order));
var changeset = serverWorld.add(order.type, 0, order.cell);
if (changeset === null) {
// not enough money
} else {
// FIXME need to get island data because money changed
changeset.island = serverWorld.dataSerialize(0);
gamejs.debug('build order, changeset ', JSON.stringify(changeset));
viewWorld.patch(changeset);
hud.patch(changeset);
buildSound().play();
}
});
shipMenu.getOrders().forEach(function(order) {
gamejs.debug('move order, sending', JSON.stringify(order));
serverWorld.setPath(order.unitId, order.path);
});
// update model
var changeset = serverWorld.update(msDuration);
// FIXME don't call model so often it only updates rarely
if (objects.keys(changeset).length) {
viewWorld.patch(changeset);
hud.patch(changeset);
}
viewWorld.update(msDuration);
display.clear();
viewWorld.draw(display);
hud.draw(display);
shipMenu.draw(display);
gamejs.draw.rect(display, 'rgba(200,200,200,0.5)', cellCursor, 0);
return;
};
var display = gamejs.display.setMode(screen);
// DEBUG
//window.statlogger = statlogger;
//window.display = display;
// return;
// DEBUG END
gamejs.setLogLevel('debug');
gamejs.debug('WorldSize: ', worldSize);
touch.init();
var mainMenu = new MainMenu();
if (isNaN(hashRounds)) {
mainMenu.show(false);
hashRounds = 0;
} else {
mainMenu.hide();
mainMenu.reset();
}
var serverWorld = new ServerWorld(worldSize, hashRounds);
var worldDump = serverWorld.serialize();
var viewWorld = new ViewWorld(worldSize, cellSize, worldDump);
var islandData = serverWorld.dataSerialize(0);
var hud = new Hud(worldDump.months, islandData);
var buildMenu = new BuildMenu();
var shipMenu = new ShipMenu(viewWorld, cellCursor);
var buildSound = function() { return new gamejs.mixer.Sound('sounds/client/impactfall.ogg'); };
gamejs.time.fpsCallback(tick, this, 15);
};