-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
86 lines (69 loc) · 1.97 KB
/
controller.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
"use strict";
var STATE_LOGGED_IN = 'Logged in',
STATE_LOGGED_OUT = 'Logged out',
STATE_LOGGING_IN = 'Logging in...';
var EVENT_LOGIN = 'game.login',
EVENT_LOGOUT = 'game.logout';
var network_client,
game_controller;
function register_event(evt, fnc){
$(document).on(evt, fnc);
}
function trigger_event(evt, args){
console.log("EVENT: "+evt)
$(document).trigger(evt, args);
view_update_status();
}
function GameController(){
var self = this;
self.state = STATE_LOGGED_OUT;
self.terrain = {};
self.objects = {};
self.creature = null;
self.logout = function(){
self.state = STATE_LOGGED_OUT;
self.terrain = {};
self.objects = {};
trigger_event(EVENT_LOGOUT);
}
register_event(EVENT_CONN_LOST, self.logout)
self.update_terrain = function(terr){
var objects = terr.objects;
delete terr.objects;
$.extend(self.terrain, terr);
view.terrain_redraw(self.terrain, self.creature.coords);
}
self.update_objects = function(obj){
$.extend(self.objects, obj);
view.draw_objects(self.objects);
}
self.walk = function(coords){
network_client.ws.send(JSON.stringify({
what:"action",
type:"walk",
where:coords
}))
}
self.do_death = function(msg){
delete self.objects[msg.who];
view.terrain_redraw(self.terrain, self.creature.coords);
}
self.attack = function(id){
network_client.ws.send(JSON.stringify({
what:"action",
type:"attack",
who:id
}))
}
self.login_fail = function(reason){
if (self.state == STATE_LOGGING_IN){
log("Login failed: "+reason);
self.logout();
}
}
}
//////////////////////////// MAIN ////////////////////////
$(function(){
network_client = new NetworkClient();
game_controller = new GameController();
});