-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFGame.as
More file actions
191 lines (153 loc) · 4.27 KB
/
FGame.as
File metadata and controls
191 lines (153 loc) · 4.27 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
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
package Framework
{
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.*;
import Framework.FG;
import Framework.FInvalidURL;
import Framework.Utils.FInternet;
import flash.utils.getTimer;
import mochi.as3.*;
public class FGame extends Sprite
{
// Scene
protected var sceneMask:Sprite;
public var useMask:Boolean;
protected var scene:FScene;
public var _requestedScene:FScene
public var switchingScene:Boolean;
public var transitionFunc:Function;
public var transitionTime:Number;
public var transitionTimeLeft:Number;
protected var gameURL:String;
public var allowedURLs:Array;
public function FGame(g:FGame, w:int, h:int, s:Class):void
{
FG.Init(g, w, h);
FG._scene = scene = new s();
_requestedScene = scene;
addChild(scene);
// Once we have a reference to Flash we can do our thing
addEventListener(Event.ADDED_TO_STAGE, Create);
}
public function Create(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, Create);
// Check if we're on an allowed website
var validURL:Boolean = false;
if(allowedURLs != null)
{
for each(var u:String in allowedURLs)
{
if(FInternet.ValidURL(this, u, true))
{
validURL = true;
break;
}
}
}
else
{
validURL = true;
}
if(!validURL)
invalidURL();
switchingScene = false;
useMask = true;
InitScene();
// If we're using Mochiads let's connect to it
if(FG._mochiads_game_id != null)
{
MochiServices.connect(FG._mochiads_game_id, root);
}
// Necessary to have the proper delta time value on first scene update call
FG.UpdateTime();
// Mouse hooks
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
// Keyboard hooks
//stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
//stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
// Think/draw hook
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
// Only called outside of this class if you want to modify the default parameters
public function InitScene():void
{
if(useMask)
{
sceneMask = new Sprite();
sceneMask.graphics.beginFill(0);
sceneMask.graphics.drawRect(0, 0, FG.width, FG.height);
sceneMask.graphics.endFill();
// Gotta be on the stage to respond to Flash player resizing
addChild(sceneMask);
scene.mask = sceneMask;
}
}
// The top level game loop
protected function onEnterFrame(e:Event):void
{
// Update the time elapsed since the last frame
FG.UpdateTime();
FG.mouse.Update();
// Switch scene
if(scene != _requestedScene && !switchingScene)
SwitchScene();
if(switchingScene)
AnimateScene();
// Do game logic
scene.Update();
// Draw all game objects
scene.Draw();
}
// Change active scene being run in the game loop
protected function SwitchScene():void
{
if(transitionTimeLeft > 0 && transitionFunc != null)
{
switchingScene = true;
addChild(_requestedScene);
_requestedScene.paused = true;
}
else
{
if(switchingScene)
{
removeChild(_requestedScene);
_requestedScene.paused = false;
}
scene.Destroy();
removeChild(scene);
scene = _requestedScene;
FG._scene = scene;
addChild(scene);
InitScene();
switchingScene = false;
}
}
protected function AnimateScene():void
{
transitionTimeLeft -= FG.dt;
if(transitionTimeLeft + FG.dt == 0 || transitionFunc == null)
SwitchScene();
// Guarantee we get to the exact end point of our transition time
if(transitionTimeLeft < 0)
transitionTimeLeft = 0;
// Paused by default, but can be switched on manually
_requestedScene.Update();
_requestedScene.Draw();
if(transitionFunc is Function)
transitionFunc(scene, _requestedScene, transitionTimeLeft / transitionTime);
}
protected function invalidURL():void
{
_requestedScene = new FInvalidURL();
}
// Pass events mouse object
protected function onMouseDown(e:MouseEvent):void { FG.mouse.onDown(e); }
protected function onMouseUp(e:MouseEvent):void { FG.mouse.onUp(e); }
protected function onMouseWheel(e:MouseEvent):void { FG.mouse.onWheel(e); }
}
}