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
d7a7a2ed
Commit
d7a7a2ed
authored
Aug 26, 2015
by
Viktor Lidholt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #859 from vlidholt/master
Adds power ups to demo game
parents
6215abec
3464bd18
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
22 deletions
+75
-22
game_demo_node.dart
examples/game/lib/game_demo_node.dart
+20
-11
game_objects.dart
examples/game/lib/game_objects.dart
+55
-11
No files found.
examples/game/lib/game_demo_node.dart
View file @
d7a7a2ed
...
...
@@ -38,7 +38,11 @@ class GameDemoNode extends NodeWithSize {
_level
=
new
Level
();
_gameScreen
.
addChild
(
_level
);
_objectFactory
=
new
GameObjectFactory
(
_spritesGame
,
_sounds
,
_level
);
// Add heads up display
_playerState
=
new
PlayerState
(
_spritesUI
);
addChild
(
_playerState
);
_objectFactory
=
new
GameObjectFactory
(
_spritesGame
,
_sounds
,
_level
,
_playerState
);
_level
.
ship
=
new
Ship
(
_objectFactory
);
_level
.
addChild
(
_level
.
ship
);
...
...
@@ -47,10 +51,6 @@ class GameDemoNode extends NodeWithSize {
_joystick
=
new
VirtualJoystick
();
_gameScreen
.
addChild
(
_joystick
);
// Add HUD
_hud
=
new
Hud
(
_spritesUI
);
addChild
(
_hud
);
// Add initial game objects
addObjects
();
}
...
...
@@ -76,7 +76,7 @@ class GameDemoNode extends NodeWithSize {
StarField
_starField
;
RepeatedImage
_background
;
RepeatedImage
_nebula
;
Hud
_hud
;
PlayerState
_playerState
;
// Game properties
double
_scrollSpeed
=
2.0
;
...
...
@@ -147,7 +147,7 @@ class GameDemoNode extends NodeWithSize {
for
(
GameObject
damageable
in
damageables
)
{
if
(
laser
.
collidingWith
(
damageable
))
{
// Hit something that can take damage
_hud
.
score
+=
damageable
.
addDamage
(
laser
.
impact
);
damageable
.
addDamage
(
laser
.
impact
);
laser
.
destroy
();
}
}
...
...
@@ -162,6 +162,11 @@ class GameDemoNode extends NodeWithSize {
killShip
();
_level
.
ship
.
visible
=
false
;
}
}
else
if
(
node
is
GameObject
&&
node
.
canBeCollected
)
{
if
(
node
.
collidingWith
(
_level
.
ship
))
{
// The ship ran over something collectable
node
.
removeFromParent
();
}
}
}
}
...
...
@@ -235,7 +240,7 @@ class GameDemoNode extends NodeWithSize {
_gameOver
=
true
;
// Return to main scene and report the score back in 2 seconds
new
Timer
(
new
Duration
(
seconds:
2
),
()
{
_gameOverCallback
(
_
hud
.
score
);
});
new
Timer
(
new
Duration
(
seconds:
2
),
()
{
_gameOverCallback
(
_
playerState
.
score
);
});
}
}
...
...
@@ -256,14 +261,16 @@ enum GameObjectType {
asteroidBig
,
asteroidSmall
,
movingEnemy
,
coin
,
}
class
GameObjectFactory
{
GameObjectFactory
(
this
.
sheet
,
this
.
sounds
,
this
.
level
);
GameObjectFactory
(
this
.
sheet
,
this
.
sounds
,
this
.
level
,
this
.
playerState
);
SpriteSheet
sheet
;
Map
<
String
,
SoundEffect
>
sounds
;
Level
level
;
PlayerState
playerState
;
void
addAsteroids
(
int
numAsteroids
,
double
yPos
,
double
distribution
)
{
for
(
int
i
=
0
;
i
<
numAsteroids
;
i
++)
{
...
...
@@ -290,6 +297,8 @@ class GameObjectFactory {
obj
=
new
AsteroidSmall
(
this
);
else
if
(
type
==
GameObjectType
.
movingEnemy
)
obj
=
new
MovingEnemy
(
this
);
else
if
(
type
==
GameObjectType
.
coin
)
obj
=
new
Coin
(
this
);
obj
.
position
=
pos
;
obj
.
setupActions
();
...
...
@@ -298,7 +307,7 @@ class GameObjectFactory {
}
}
class
Hud
extends
Node
{
class
PlayerState
extends
Node
{
SpriteSheet
sheet
;
Sprite
sprtBgScore
;
...
...
@@ -312,7 +321,7 @@ class Hud extends Node {
_dirtyScore
=
true
;
}
Hud
(
this
.
sheet
)
{
PlayerState
(
this
.
sheet
)
{
position
=
new
Point
(
310.0
,
10.0
);
scale
=
0.6
;
...
...
examples/game/lib/game_objects.dart
View file @
d7a7a2ed
part of
game
;
abstract
class
GameObject
extends
Node
{
GameObject
(
this
.
f
);
double
radius
=
0.0
;
double
removeLimit
=
1280.0
;
bool
canDamageShip
=
true
;
bool
canBeDamaged
=
true
;
bool
canBeCollected
=
false
;
double
maxDamage
=
3.0
;
double
damage
=
0.0
;
final
GameObjectFactory
f
;
Paint
_paintDebug
=
new
Paint
()
..
color
=
new
Color
(
0xffff0000
)
..
strokeWidth
=
1.0
...
...
@@ -37,25 +42,35 @@ abstract class GameObject extends Node {
parent
.
addChild
(
explo
);
}
PowerUp
powerUp
=
createPowerUp
();
if
(
powerUp
!=
null
)
{
powerUp
.
position
=
position
;
powerUp
.
setupActions
();
parent
.
addChild
(
powerUp
);
}
removeFromParent
();
}
}
int
addDamage
(
double
d
)
{
if
(!
canBeDamaged
)
return
0
;
void
addDamage
(
double
d
)
{
if
(!
canBeDamaged
)
return
;
damage
+=
d
;
if
(
damage
>=
maxDamage
)
{
destroy
();
return
(
maxDamage
*
10
).
ceil
();
f
.
playerState
.
score
+=
(
maxDamage
*
10
).
ceil
();
}
return
10
;
}
Explosion
createExplosion
()
{
return
null
;
}
PowerUp
createPowerUp
()
{
return
null
;
}
void
paint
(
PaintingCanvas
canvas
)
{
if
(
_drawDebug
)
{
canvas
.
drawCircle
(
Point
.
origin
,
radius
,
_paintDebug
);
...
...
@@ -68,7 +83,7 @@ abstract class GameObject extends Node {
}
class
Ship
extends
GameObject
{
Ship
(
GameObjectFactory
f
)
{
Ship
(
GameObjectFactory
f
)
:
super
(
f
)
{
// Add main ship sprite
_sprt
=
new
Sprite
(
f
.
sheet
[
"ship.png"
]);
_sprt
.
scale
=
0.3
;
...
...
@@ -99,7 +114,7 @@ class Ship extends GameObject {
class
Laser
extends
GameObject
{
double
impact
=
1.0
;
Laser
(
GameObjectFactory
f
)
{
Laser
(
GameObjectFactory
f
)
:
super
(
f
)
{
// Add sprite
_sprt
=
new
Sprite
(
f
.
sheet
[
"laser.png"
]);
_sprt
.
scale
=
0.3
;
...
...
@@ -121,14 +136,13 @@ class Laser extends GameObject {
abstract
class
Obstacle
extends
GameObject
{
Obstacle
(
this
.
_
f
);
Obstacle
(
GameObjectFactory
f
)
:
super
(
f
);
double
explosionScale
=
1.0
;
GameObjectFactory
_f
;
Explosion
createExplosion
()
{
SoundEffectPlayer
.
sharedInstance
().
play
(
_
f
.
sounds
[
"explosion"
]);
Explosion
explo
=
new
Explosion
(
_
f
.
sheet
);
SoundEffectPlayer
.
sharedInstance
().
play
(
f
.
sounds
[
"explosion"
]);
Explosion
explo
=
new
Explosion
(
f
.
sheet
);
explo
.
scale
=
explosionScale
;
return
explo
;
}
...
...
@@ -154,6 +168,10 @@ abstract class Asteroid extends Obstacle {
int
alpha
=
((
200.0
*
d
)
~/
maxDamage
).
clamp
(
0
,
200
);
_sprt
.
colorOverlay
=
new
Color
.
fromARGB
(
alpha
,
255
,
3
,
86
);
}
PowerUp
createPowerUp
()
{
return
new
Coin
(
f
);
}
}
class
AsteroidBig
extends
Asteroid
{
...
...
@@ -181,7 +199,7 @@ class MovingEnemy extends Obstacle {
_sprt
=
new
Sprite
(
f
.
sheet
[
"ship.png"
]);
_sprt
.
scale
=
0.2
;
radius
=
12.0
;
maxDamage
=
2
.0
;
maxDamage
=
1
.0
;
addChild
(
_sprt
);
constraints
=
[
new
ConstraintRotationToMovement
(
dampening:
0.5
)];
...
...
@@ -231,3 +249,29 @@ class MovingEnemy extends Obstacle {
Sprite
_sprt
;
}
class
PowerUp
extends
GameObject
{
PowerUp
(
GameObjectFactory
f
)
:
super
(
f
)
{
canDamageShip
=
false
;
canBeDamaged
=
false
;
canBeCollected
=
true
;
}
}
class
Coin
extends
PowerUp
{
Coin
(
GameObjectFactory
f
)
:
super
(
f
)
{
_sprt
=
new
Sprite
(
f
.
sheet
[
"shield.png"
]);
_sprt
.
transferMode
=
sky
.
TransferMode
.
plus
;
_sprt
.
size
=
new
Size
(
15.0
,
15.0
);
addChild
(
_sprt
);
radius
=
7.5
;
}
setupActions
()
{
ActionTween
rotate
=
new
ActionTween
((
a
)
=>
_sprt
.
rotation
=
a
,
0.0
,
360.0
,
1.0
);
actions
.
run
(
new
ActionRepeatForever
(
rotate
));
}
Sprite
_sprt
;
}
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