-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvent.js
More file actions
148 lines (127 loc) · 4.37 KB
/
Event.js
File metadata and controls
148 lines (127 loc) · 4.37 KB
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
/*global MainGame*/
/*global DDecisionView*/
/*global DPageIndicator*/
/*global DUiMask*/
// Design Pattern:
// Texture requirement: 'event_bg'
var Event={
createNew: function(isInformation){
var v = MainGame.game.add.sprite(0, 0, "event_bg");
// Center by default
v.position.set(MainGame.game.width/2 - v.width/2, MainGame.game.height/2 - v.height/2);
// add bg
v.bg = MainGame.game.add.sprite(0, 0, "event_bg");
v.addChild(v.bg);
// add decision view
v.decisionView = DDecisionView.createNew();
v.addChild(v.decisionView);
// add page inidcator if necessary
v.pageIndicator=DPageIndicator.createNew(400, {x:200,y:0});
v.addChild(v.pageIndicator);
v.pageIndicator.position.set(0,200);
v.pageIndicator.setController(function(curPage){Event._onPageChanged_(v,curPage)}, 110);
if(!isInformation)
v.pageIndicator.visible=false;
v.isInformation=isInformation;
// setup priority
v.inputEnabled = true;
v.input.priorityID = 101;
// setup a mask (not child)
v.uiMask = DUiMask.createNew();
v.uiMask.setController(100, function(){
if(isInformation){
v.uiMask.destroy();
v.destroy();
}
});
// Class funcs
/* set the model (data) of this event view. The model is an array of:
{portrait:"myTextureKey",
description:"Yi: How are you?",
buttonTexts:["Good!","Not well."]} */
v.setModel=function(events,_curPage){Event.setModel(v,events,_curPage)};
/* sets the controller of this event view. The controller is an array of:
[function(){ev.gotoPage(1)}, function(){ev.gotoPage(2)}]
The count in each element should equal to the count of the relative buttonTexts*/
v.setController=function(callbacks){Event.setController(v,callbacks)};
// goes to page [index]
v.gotoPage=function(index){Event.gotoPage(v,index)};
// removes this event view
v.suicide=function(){v.uiMask.destroy();v.destroy()};
return v;
},
setModel: function(v, events, _curPage){
v.myModel=events;
// set page indicator's pageCount & curPage
_curPage=(_curPage?_curPage:0);
v.pageIndicator.setModel(_curPage, events.length);
// set model of decision view
var mod=v.myModel[_curPage];
v.decisionView.setModel(mod.portrait, mod.description, mod.buttonTexts);
},
setController: function(v, callbacks){
v.myController=callbacks;
// set the callback for current page
var curPage=v.pageIndicator.curPage;
v.decisionView.setController(120, v.myController[curPage]);
},
gotoPage: function(v, index){
console.assert(index>=0 && index<v.myModel.length);
// reset decision view
var mod=v.myModel[index];
v.decisionView.setModel(mod.portrait,mod.description,mod.buttonTexts);
if(!v.isInformation){
var cb=v.myController[index];
v.decisionView.setController(120, cb);
}
// reset page indicator
v.pageIndicator.curPage=index;
},
_onPageChanged_: function(v, curPage){
console.log("Event: page changed:",curPage);
// reset decision view
Event.gotoPage(v, curPage);
},
};
function test_Event(){
var e=Event.createNew();
e.position.set(50,50);
e.setModel([
{portrait:'bureaucrat_port_0',description:"Yi: please select a test.",buttonTexts:["Choice","Info"]}
]);
e.setController([
[function(){
e.suicide();
_test_Event_choice();
}, function(){
e.suicide();
_test_Event_info();
}]
]);
}
// Private test functions //
function _test_Event_choice(){
var eChoice=Event.createNew();
eChoice.position.set(300,100);
eChoice.setModel([
{portrait:'bureaucrat_port_0',description:'Yi: Hey, MJ has a \nquestion.',buttonTexts:["Go ahead"]},
{portrait:'bureaucrat_port_2',description:'MJ: Are you a dumbass?',buttonTexts:["Yes","NO"]},
{portrait:'bureaucrat_port_2',description:'MJ: Of course you are!',buttonTexts:["Close"]},
{portrait:'bureaucrat_port_2',description:'MJ: What? You are not?',buttonTexts:["Close"]}
]);
eChoice.setController([
[function(){eChoice.gotoPage(1)}],
[function(){eChoice.gotoPage(2)}, function(){eChoice.gotoPage(3)}],
[function(){eChoice.suicide()}],
[function(){eChoice.suicide()}]
]);
}
function _test_Event_info(){
var eInfo=Event.createNew(true);
eInfo.position.set(250,150);
eInfo.setModel([
{portrait:'bureaucrat_port_0',description:"Yi: Your people are \nangrier.",buttonTexts:[]},
{portrait:'bureaucrat_port_2',description:"MJ: Your people are \nfreer.",buttonTexts:[]},
{portrait:'bureaucrat_port_0',description:"Yi: ...and you are \nbroke",buttonTexts:[]}
]);
}