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
|
/* <license>
* This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
* For full copyright and license information please refer to doc/license.txt.
* </license>
*/
package squix;
import mathx.Point2;
import squix.CursorActor;
class PlayerCursor extends CorePanelObject
{
var speed : Float;
var speedFactor : Float;
var speedTime : Float;
public function new( game : Game )
{
super(
game,
new CursorActor( { marqueeSpeed: 2 } ),
game.global.cursorSize
);
game.cursor = this;
place( Point2.at( game.surface.width /2, game.surface.height ) );
if( game.cursorStartOnEdge )
place( game.surface.findNearestEdge( sAt.roundDemote() ).promoteF() );
alive = false;
actor.actorSetSequence( CursorActor.seq.Create, ui.Action.bind( setAlive ) );
speed = game.global.cursorSpeed;
speedFactor = 1;
game.addEventListener( GameEvent.CursorDie, onCursorDie );
game.addEventListener( GameEvent.BonusCollected, onBonusCollected );
}
/**
* Determines whether this cursor is in a "usable" state, that is, it
* is alive and can be regarded as a true cursor. Introduced so that
* the TraceSelector would not use a cursor which is being created
* or dying.
*/
public function isUsable() : Bool
{
return alive;
}
public function getSpeed() : Float
{
return speed * speedFactor;
}
override function onBonusCollected( ge : GameEvent )
{
switch( ge.bonus )
{
case CursorSpeedUp:
speedFactor = game.global.cursorSpeedUpFactor;
speedTime = game.global.cursorSpeedRecoverTime;
actor.actorSetTrait1( CursorActor.trait.Marquee.Speed, 4 );
case CursorSpeedDown:
speedFactor = game.global.cursorSpeedDownFactor;
speedTime = game.global.cursorSpeedRecoverTime;
actor.actorSetTrait1( CursorActor.trait.Marquee.Speed, 0.5);
case MonsterFreeze:
//not for us
}
}
var invincibleTime : Float;
function setAlive()
{
alive = true;
//check if near enemies...
if( game.isCursorNearEdgeKiller() )
{
invincibleTime = game.global.cursorInvincibleTime;
actor.actorSetTrait( CursorActor.trait.Invincible.On );
}
else
invincibleTime = 0;
}
override function _stepTime( elapsed : Float )
{
//only move in normal sequence mode
if( !alive )
return;
//check for end of speedFactor
if( speedTime > 0 )
{
speedTime -= elapsed;
if( speedTime <= 0 )
{
speedFactor = 1;
actor.actorSetTrait1( CursorActor.trait.Marquee.Speed, 2 );
}
}
//check for turning off invincible
if( invincibleTime > 0)
{
invincibleTime -= elapsed;
if( invincibleTime <= 0 )
actor.actorSetTrait( CursorActor.trait.Invincible.Off );
}
if( game.cursorPhase == CursorPhase.Edge )
actor.actorSetTrait( CursorActor.trait.Mode.Edge );
else if( game.cursorPhase == CursorPhase.Select )
actor.actorSetTrait( CursorActor.trait.Mode.Select );
if( game.cursorPhase != CursorPhase.Fill
&& (!game.lockCursorOnSelector || game.cursorPhase == CursorPhase.Edge ) )
moveCursor( elapsed );
}
function moveCursor( elapsed : Float )
{
//Same basic structure as in TraceSelector
var pos = targetCursorPos();
var dirs = game.surface.getCursorDirections( sAt );
var cdir = GridUtil.pointClosest( pos, dirs, game.allowCursorJitter ? null : sAt );
//if none then we'll just drift to where we need to be
var cnext;
if( cdir == null )
{
//TODO: if dirs.length != 0, then check one more step to see which direction would lead us
//closed to the cursor (less sticky movement then (one-blocks don't halt the cursor then)
if( dirs.length !=0 ) //only if there was really *no* choice (as there might be two options, neither of which are closer to the mouse)
return;
if( game.surface.isCovered( sAt.floorDemote() ) ) //only while in open area, should be locked when not... (shouldn't happen)
{
//snap to nearest intersection (to keep valid state)
place( sAt.round() ); //TODO: snap to x or y bound only!
return;
}
//otherwise move towards destination (TODO: max move into new cell, use div 2 now to come close...)
//TODO: max the actual distance to mouse as well, otherwise jitters
cnext = sAt.unitVectorTo( pos ).div(2).add( sAt );
}
else
cnext = cdir.mp;
var step = GridUtil.excessStep( sAt, cnext, elapsed * getSpeed() );
//prevent off-surface wandering (Due here to actually have some elapsed movement -- see plan defect)
var next = step.next.constrain( Point2.at( 0, 0 ), Point2.at( game.surface.width, game.surface.height ) );
place( next );
if( step.excess > 0 ) //perhaps should use !isZero
{
moveCursor( step.excess / getSpeed() );
return;
}
}
override public function moveObject( smp : Point2 )
{
//assume location is valid.
place( smp );
}
function onCursorDie( ge : GameEvent )
{
alive = false;
actor.actorSetSequence( CursorActor.seq.Destroy, ui.Action.bind( destroy ) );
flashx.SoundManager.playSound( "playerDies" );
}
override function destroy()
{
super.destroy();
game.cursor = null;
game.removeEventListener( GameEvent.CursorDie, onCursorDie );
}
override function onLevelOver( ge : GameEvent )
{
game.numLives++; //we're returning to the player's lives remaining
super.onLevelOver( ge );
}
override public function getCorePanelCollisionObject( ) : flash.display.DisplayObject
{
if( !alive || invincibleTime > 0 )
return null;
return actor.getNative();
}
}
|