My favorites | Sign in
Project Logo
       
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
*
* MIT License
*
* Copyright (c) 2008, Juan Delgado - Zarate
*
* http://zarate.tv/projects/zcode/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

import tv.zarate.Projects.Snake.SnakeView;
import tv.zarate.Projects.Snake.SnakeData;
import tv.zarate.Projects.Snake.Worlds.*;
import tv.zarate.Utils.Delegate;
import tv.zarate.Utils.Engine;

class tv.zarate.Projects.Snake.SnakeModel{

private var view:SnakeView;
private var world:SnakeWorld;
private var beatInterval:Number = 0;
private var beatTime:Number = 200;
private var xDir:Number = 1;
private var yDir:Number = 0;
private var engine:Engine;
private var divide:Number = 1;
private var counter:Number = 0;
private var toEngine:Function;
private var nextCallback:Function;
private var data:SnakeData;
private var maxScore:Number = 0;
private var paused:Boolean = true;

public function SnakeModel(){

engine = Engine.getInstance();
toEngine = Delegate.create(this,updateWorld);
data = new SnakeData();

}

public function config(_view:SnakeView,m:MovieClip):Void{

view = _view;
engine.config(m,500);

}

public function start():Void{
data.getMaxScore(Delegate.create(this,scoreLoaded));
}

public function changeDir(_xDir:Number,_yDir:Number):Void{

if(!paused){

xDir = _xDir;
yDir = _yDir;

}

}

public function nextScreen():Void{

nextCallback();

}

public function pause():Void{

// you are not going to stop it after maxScore, coward!
if(world.score < maxScore) manageEngine(paused);

}

/* ************************* PRIVATE METHODS ************************* */

private function scoreLoaded(success:Boolean,score:Number):Void{

if(success){

// when thousands of developers make more and more engines
// we could get this from xml, db or whatever

var modes:Array = new Array();
modes.push(
{title:"Classic Mode",
description:"This is a good one to start with... by the way.... po-po-po!",
value:"classic"},
{title:"Panic Mode",
description:"Yeah! :D",
value:"panic"}
);

if(score != undefined){

maxScore = score;
view.maxScore = score;

}

view.drawInitialScreen(modes,Delegate.create(this,setEngine),Delegate.create(this,changeKeys));

} else {

trace("Impossible to load previous score");
// TODO, display properly in the view

}

}

private function setEngine(engineType:String,showInstructions:Boolean):Void{

// yes, probably one nice design pattern would do a better job here,
// but we will wait to those thousands of developers

switch(engineType){

case("classic"):
world = Classic.getInstance();
break;
case("panic"):
world = Panic.getInstance();
break;

}

if(showInstructions){

view.showInstructionsScreen(world.instructions,Delegate.create(this,configWorld),Delegate.create(this,scoreLoaded,true));

} else{

configWorld();

}


}

private function changeKeys():Void{

view.drawChangeKeysScreen();

}

private function configWorld():Void{

xDir = 1;
yDir = 0;

world.config(Delegate.create(this,gameOver));
world.initWorld();

view.gameOver = false;
view.drawPlayingScreen(world.width,world.height);
view.drawWorld(world);

manageEngine(true);

}

private function manageEngine(action:Boolean):Void{

if(action){
engine.addListener(toEngine);
} else {
engine.removeListener(toEngine);
}

paused = !action;

}

private function updateWorld():Void{

counter++;

if((counter % divide) == 0){

counter = 0;
world.update(xDir,yDir);
view.drawWorld(world);

}

}

private function gameOver(userScore:Number):Void{

manageEngine(false);
view.gameOver = true;

if(userScore > maxScore){

maxScore = userScore;
view.maxScore = userScore;
data.setMaxScore(userScore,Delegate.create(this,updateMaxScore));

} else {

updateMaxScore();

}

}

private function updateMaxScore(success:Boolean,newMaxScore:Number,maxScoreDate:Date):Void{

view.drawGameOverScreen(world.score,newMaxScore,maxScoreDate,Delegate.create(this,configWorld));
nextCallback = Delegate.create(this,configWorld);

}

}
Show details Hide details

Change log

r120 by zzzarate on Aug 14, 2008   Diff
ZCode now released under MIT license.
Enjoy.
Go to: 
Project members, sign in to write a code review

Older revisions

r80 by zzzarate on Mar 01, 2008   Diff
Half way through the initial version
and the final one with different
worlds or modes. Probably doesn't even
compile, but don't want to keep it
outside the rep for more time.
...
All revisions of this file

File info

Size: 5505 bytes, 235 lines

File properties

svn:executable
*
Hosted by Google Code