Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
138e8fbf
Commit
138e8fbf
authored
Aug 28, 2015
by
Viktor Lidholt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds power-ups to demo game
parent
c7d2872d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
154 additions
and
18 deletions
+154
-18
game_demo_node.dart
examples/game/lib/game_demo_node.dart
+21
-6
game_object_factory.dart
examples/game/lib/game_object_factory.dart
+4
-0
game_objects.dart
examples/game/lib/game_objects.dart
+103
-12
player_state.dart
examples/game/lib/player_state.dart
+26
-0
No files found.
examples/game/lib/game_demo_node.dart
View file @
138e8fbf
...
...
@@ -45,6 +45,7 @@ class GameDemoNode extends NodeWithSize {
_objectFactory
=
new
GameObjectFactory
(
_spritesGame
,
_sounds
,
_level
,
_playerState
);
_level
.
ship
=
new
Ship
(
_objectFactory
);
_level
.
ship
.
setupActions
();
_level
.
addChild
(
_level
.
ship
);
// Add the joystick
...
...
@@ -111,7 +112,7 @@ class GameDemoNode extends NodeWithSize {
// Add shots
if
(
_framesToFire
==
0
&&
_joystick
.
isDown
&&
!
_gameOver
)
{
fire
();
_framesToFire
=
_framesBetweenShots
;
_framesToFire
=
(
_playerState
.
speedLaserActive
)
?
_framesBetweenShots
~/
2
:
_framesBetweenShots
;
}
if
(
_framesToFire
>
0
)
_framesToFire
--;
...
...
@@ -158,9 +159,13 @@ class GameDemoNode extends NodeWithSize {
for
(
Node
node
in
nodes
)
{
if
(
node
is
GameObject
&&
node
.
canDamageShip
)
{
if
(
node
.
collidingWith
(
_level
.
ship
))
{
// The ship was hit :(
killShip
();
_level
.
ship
.
visible
=
false
;
if
(
_playerState
.
shieldActive
)
{
// Hit, but saved by the shield!
node
.
destroy
();
}
else
{
// The ship was hit :(
killShip
();
}
}
}
else
if
(
node
is
GameObject
&&
node
.
canBeCollected
)
{
if
(
node
.
collidingWith
(
_level
.
ship
))
{
...
...
@@ -209,14 +214,24 @@ class GameDemoNode extends NodeWithSize {
}
void
fire
()
{
Laser
shot0
=
new
Laser
(
_objectFactory
);
Laser
shot0
=
new
Laser
(
_objectFactory
,
-
90.0
);
shot0
.
position
=
_level
.
ship
.
position
+
new
Offset
(
17.0
,
-
10.0
);
_level
.
addChild
(
shot0
);
Laser
shot1
=
new
Laser
(
_objectFactory
);
Laser
shot1
=
new
Laser
(
_objectFactory
,
-
90.0
);
shot1
.
position
=
_level
.
ship
.
position
+
new
Offset
(-
17.0
,
-
10.0
);
_level
.
addChild
(
shot1
);
if
(
_playerState
.
sideLaserActive
)
{
Laser
shot2
=
new
Laser
(
_objectFactory
,
0.0
);
shot2
.
position
=
_level
.
ship
.
position
+
new
Offset
(
17.0
,
-
10.0
);
_level
.
addChild
(
shot2
);
Laser
shot3
=
new
Laser
(
_objectFactory
,
180.0
);
shot3
.
position
=
_level
.
ship
.
position
+
new
Offset
(-
17.0
,
-
10.0
);
_level
.
addChild
(
shot3
);
}
_effectPlayer
.
play
(
_sounds
[
"laser"
]);
}
...
...
examples/game/lib/game_object_factory.dart
View file @
138e8fbf
...
...
@@ -3,6 +3,7 @@ part of game;
enum
GameObjectType
{
asteroidBig
,
asteroidSmall
,
asteroidPowerUp
,
enemyScout
,
enemyDestroyer
,
coin
,
...
...
@@ -19,6 +20,7 @@ class GameObjectFactory {
void
addAsteroids
(
int
numAsteroids
,
double
yPos
,
double
distribution
)
{
for
(
int
i
=
0
;
i
<
numAsteroids
;
i
++)
{
GameObjectType
type
=
(
randomDouble
()
<
distribution
)
?
GameObjectType
.
asteroidBig
:
GameObjectType
.
asteroidSmall
;
if
(
i
==
0
)
type
=
GameObjectType
.
asteroidPowerUp
;
Point
pos
=
new
Point
(
randomSignedDouble
()
*
160.0
,
yPos
+
_chunkSpacing
*
randomDouble
());
addGameObject
(
type
,
pos
);
...
...
@@ -45,6 +47,8 @@ class GameObjectFactory {
obj
=
new
AsteroidBig
(
this
);
else
if
(
type
==
GameObjectType
.
asteroidSmall
)
obj
=
new
AsteroidSmall
(
this
);
else
if
(
type
==
GameObjectType
.
asteroidPowerUp
)
obj
=
new
AsteroidPowerUp
(
this
);
else
if
(
type
==
GameObjectType
.
enemyScout
)
obj
=
new
EnemyScout
(
this
);
else
if
(
type
==
GameObjectType
.
enemyDestroyer
)
...
...
examples/game/lib/game_objects.dart
View file @
138e8fbf
...
...
@@ -42,7 +42,7 @@ abstract class GameObject extends Node {
parent
.
addChild
(
explo
);
}
PowerUp
powerUp
=
createPowerUp
();
Collectable
powerUp
=
createPowerUp
();
if
(
powerUp
!=
null
)
{
powerUp
.
position
=
position
;
powerUp
.
setupActions
();
...
...
@@ -71,7 +71,7 @@ abstract class GameObject extends Node {
return
null
;
}
PowerUp
createPowerUp
()
{
Collectable
createPowerUp
()
{
return
null
;
}
...
...
@@ -93,8 +93,13 @@ class Ship extends GameObject {
_sprt
.
scale
=
0.3
;
_sprt
.
rotation
=
-
90.0
;
addChild
(
_sprt
);
radius
=
20.0
;
_sprtShield
=
new
Sprite
(
f
.
sheet
[
"shield.png"
]);
_sprtShield
.
scale
=
0.35
;
_sprtShield
.
transferMode
=
sky
.
TransferMode
.
plus
;
addChild
(
_sprtShield
);
radius
=
20.0
;
canBeDamaged
=
false
;
canDamageShip
=
false
;
...
...
@@ -103,6 +108,7 @@ class Ship extends GameObject {
}
Sprite
_sprt
;
Sprite
_sprtShield
;
void
applyThrust
(
Point
joystickValue
,
double
scroll
)
{
Point
oldPos
=
position
;
...
...
@@ -113,29 +119,51 @@ class Ship extends GameObject {
GameMath
.
filter
(
oldPos
.
x
,
target
.
x
,
filterFactor
),
GameMath
.
filter
(
oldPos
.
y
,
target
.
y
,
filterFactor
));
}
void
setupActions
()
{
ActionTween
rotate
=
new
ActionTween
((
a
)
=>
_sprtShield
.
rotation
=
a
,
0.0
,
360.0
,
1.0
);
_sprtShield
.
actions
.
run
(
new
ActionRepeatForever
(
rotate
));
}
void
update
(
double
dt
)
{
// Update shield
if
(
f
.
playerState
.
shieldActive
)
{
if
(
f
.
playerState
.
shieldDeactivating
)
_sprtShield
.
visible
=
!
_sprtShield
.
visible
;
else
_sprtShield
.
visible
=
true
;
}
else
{
_sprtShield
.
visible
=
false
;
}
}
}
class
Laser
extends
GameObject
{
double
impact
=
1.0
;
Laser
(
GameObjectFactory
f
)
:
super
(
f
)
{
Laser
(
GameObjectFactory
f
,
double
r
)
:
super
(
f
)
{
// Add sprite
_sprt
=
new
Sprite
(
f
.
sheet
[
"explosion_particle.png"
]);
_sprt
.
scale
=
0.5
;
_sprt
.
colorOverlay
=
new
Color
(
0xff95f4fb
);
_sprt
.
transferMode
=
sky
.
TransferMode
.
plus
;
_sprt
.
rotation
=
r
+
90.0
;
addChild
(
_sprt
);
radius
=
10.0
;
removeLimit
=
640.0
;
canDamageShip
=
false
;
canBeDamaged
=
false
;
_offset
=
new
Offset
(
math
.
cos
(
radians
(
r
))
*
10.0
,
math
.
sin
(
radians
(
r
))
*
10.0
);
}
Sprite
_sprt
;
Offset
_offset
;
void
move
()
{
position
+=
new
Offset
(
0.0
,
-
10.0
)
;
position
+=
_offset
;
}
}
...
...
@@ -178,7 +206,7 @@ abstract class Asteroid extends Obstacle {
_sprt
.
colorOverlay
=
colorForDamage
(
d
,
maxDamage
);
}
PowerUp
createPowerUp
()
{
Collectable
createPowerUp
()
{
return
new
Coin
(
f
);
}
}
...
...
@@ -203,6 +231,14 @@ class AsteroidSmall extends Asteroid {
}
}
class
AsteroidPowerUp
extends
AsteroidBig
{
AsteroidPowerUp
(
GameObjectFactory
f
)
:
super
(
f
);
Collectable
createPowerUp
()
{
return
new
PowerUp
(
f
,
nextPowerUpType
());
}
}
class
EnemyScout
extends
Obstacle
{
EnemyScout
(
GameObjectFactory
f
)
:
super
(
f
)
{
_sprt
=
new
Sprite
(
f
.
sheet
[
"enemy_scout_0.png"
]);
...
...
@@ -256,7 +292,7 @@ class EnemyScout extends Obstacle {
actions
.
run
(
new
ActionRepeatForever
(
spline
));
}
PowerUp
createPowerUp
()
{
Collectable
createPowerUp
()
{
return
new
Coin
(
f
);
}
...
...
@@ -286,7 +322,7 @@ class EnemyDestroyer extends Obstacle {
actions
.
run
(
new
ActionRepeatForever
(
circle
));
}
PowerUp
createPowerUp
()
{
Collectable
createPowerUp
()
{
return
new
Coin
(
f
);
}
...
...
@@ -337,15 +373,17 @@ class EnemyLaser extends Obstacle {
}
}
class
PowerUp
extends
GameObject
{
PowerUp
(
GameObjectFactory
f
)
:
super
(
f
)
{
class
Collectable
extends
GameObject
{
Collectable
(
GameObjectFactory
f
)
:
super
(
f
)
{
canDamageShip
=
false
;
canBeDamaged
=
false
;
canBeCollected
=
true
;
zPosition
=
20.0
;
}
}
class
Coin
extends
PowerUp
{
class
Coin
extends
Collectable
{
Coin
(
GameObjectFactory
f
)
:
super
(
f
)
{
_sprt
=
new
Sprite
(
f
.
sheet
[
"coin.png"
]);
_sprt
.
scale
=
0.7
;
...
...
@@ -354,9 +392,14 @@ class Coin extends PowerUp {
radius
=
7.5
;
}
setupActions
()
{
void
setupActions
()
{
// Rotate
ActionTween
rotate
=
new
ActionTween
((
a
)
=>
_sprt
.
rotation
=
a
,
0.0
,
360.0
,
1.0
);
actions
.
run
(
new
ActionRepeatForever
(
rotate
));
// Fade in
ActionTween
fadeIn
=
new
ActionTween
((
a
)
=>
_sprt
.
opacity
=
a
,
0.0
,
1.0
,
0.6
);
actions
.
run
(
fadeIn
);
}
Sprite
_sprt
;
...
...
@@ -366,3 +409,51 @@ class Coin extends PowerUp {
super
.
collect
();
}
}
enum
PowerUpType
{
shield
,
speedLaser
,
sideLaser
,
}
List
<
PowerUpType
>
_powerUpTypes
=
new
List
.
from
(
PowerUpType
.
values
);
int
_lastPowerUp
=
_powerUpTypes
.
length
;
PowerUpType
nextPowerUpType
(
)
{
if
(
_lastPowerUp
>=
_powerUpTypes
.
length
)
{
_powerUpTypes
.
shuffle
();
_lastPowerUp
=
0
;
}
PowerUpType
type
=
_powerUpTypes
[
_lastPowerUp
];
_lastPowerUp
++;
return
type
;
}
class
PowerUp
extends
Collectable
{
PowerUp
(
GameObjectFactory
f
,
this
.
type
)
:
super
(
f
)
{
_sprt
=
new
Sprite
(
f
.
sheet
[
"coin.png"
]);
_sprt
.
scale
=
1.2
;
addChild
(
_sprt
);
radius
=
10.0
;
}
Sprite
_sprt
;
PowerUpType
type
;
void
setupActions
()
{
ActionTween
rotate
=
new
ActionTween
((
a
)
=>
_sprt
.
rotation
=
a
,
0.0
,
360.0
,
1.0
);
actions
.
run
(
new
ActionRepeatForever
(
rotate
));
// Fade in
ActionTween
fadeIn
=
new
ActionTween
((
a
)
=>
_sprt
.
opacity
=
a
,
0.0
,
1.0
,
0.6
);
actions
.
run
(
fadeIn
);
}
void
collect
()
{
f
.
playerState
.
activatePowerUp
(
type
);
super
.
collect
();
}
}
examples/game/lib/player_state.dart
View file @
138e8fbf
...
...
@@ -71,6 +71,26 @@ class PlayerState extends Node {
addChild
(
sprt
);
}
void
activatePowerUp
(
PowerUpType
type
)
{
if
(
type
==
PowerUpType
.
shield
)
{
_shieldFrames
+=
300
;
}
else
if
(
type
==
PowerUpType
.
sideLaser
)
{
_sideLaserFrames
+=
300
;
}
else
if
(
type
==
PowerUpType
.
speedLaser
)
{
_speedLaserFrames
+=
300
;
}
}
int
_shieldFrames
=
0
;
bool
get
shieldActive
=>
_shieldFrames
>
0
;
bool
get
shieldDeactivating
=>
_shieldFrames
>
0
&&
_shieldFrames
<
60
;
int
_sideLaserFrames
=
0
;
bool
get
sideLaserActive
=>
_sideLaserFrames
>
0
;
int
_speedLaserFrames
=
0
;
bool
get
speedLaserActive
=>
_speedLaserFrames
>
0
;
void
flashBgSprite
(
Sprite
sprt
)
{
sprt
.
actions
.
stopAll
();
ActionTween
flash
=
new
ActionTween
(
...
...
@@ -80,6 +100,12 @@ class PlayerState extends Node {
0.3
);
sprt
.
actions
.
run
(
flash
);
}
void
update
(
double
dt
)
{
if
(
_shieldFrames
>
0
)
_shieldFrames
--;
if
(
_sideLaserFrames
>
0
)
_sideLaserFrames
--;
if
(
_speedLaserFrames
>
0
)
_speedLaserFrames
--;
}
}
class
ScoreDisplay
extends
Node
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment