Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Tomás Teijeiro Campo
citius-invaders
Commits
a6dd0b9c
Commit
a6dd0b9c
authored
Jun 08, 2016
by
Pablo R. Mier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Invaders moved to js/Invader.js
parent
15faa5d6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
88 additions
and
81 deletions
+88
-81
Game.js
Game.js
+1
-77
index.html
index.html
+1
-0
js/Invader.js
js/Invader.js
+78
-0
js/Player.js
js/Player.js
+7
-3
js/Utils.js
js/Utils.js
+1
-1
No files found.
Game.js
View file @
a6dd0b9c
...
...
@@ -7,81 +7,6 @@ var MIN_INVADERS = 4;
var
INITIAL_INVADERS
=
6
;
var
MIN_GENERATION_TIME
=
Phaser
.
Time
.
SECOND
*
2
;
// Extended sprite object for Invaders
Invader
=
function
(
ctx
,
genes
,
x
,
y
)
{
var
game
=
ctx
.
game
;
x
=
x
||
game
.
world
.
randomX
;
y
=
y
||
game
.
world
.
randomY
%
(
game
.
world
.
height
-
WALL_MARGIN
*
1.5
)
;
Phaser
.
Sprite
.
call
(
this
,
game
,
x
,
y
,
'
invader
'
);
game
.
physics
.
enable
(
this
,
Phaser
.
Physics
.
ARCADE
);
// Initialize genes by getting the default values from settings.json
this
.
genes
=
genes
||
function
()
{
var
settings
=
ctx
.
settings
;
var
genes
=
{};
for
(
var
gene
in
settings
.
genes
){
genes
[
gene
]
=
game
.
rnd
.
realInRange
(
settings
.
genes
[
gene
].
min
,
settings
.
genes
[
gene
].
max
);
}
return
genes
;
}();
this
.
anchor
.
setTo
(
0.5
,
0.5
);
var
alpha
=
Math
.
round
(
this
.
genes
[
'
alpha
'
]);
this
.
tint
=
Phaser
.
Color
.
getColor
(
alpha
,
alpha
,
alpha
);
this
.
body
.
velocity
.
x
=
this
.
genes
[
'
xvelocity
'
];
this
.
body
.
velocity
.
y
=
this
.
genes
[
'
yvelocity
'
];
this
.
scale
.
setTo
(
this
.
genes
[
'
scale
'
],
this
.
genes
[
'
scale
'
]);
this
.
body
.
collideWorldBounds
=
true
;
this
.
body
.
bounce
.
set
(
1
);
// Used to control the probability of x-y change in direction
this
.
lastTimeChanged
=
0
;
// Create a shield
var
shield
=
this
.
game
.
make
.
graphics
(
0
,
0
);
shield
.
lineStyle
(
1
,
0x15AFF0
,
1
);
shield
.
drawCircle
(
-
0.5
,
-
0.5
,
22
);
//shield.anchor.setTo(0.5, 0.5);
this
.
addChild
(
shield
);
shield
.
visible
=
false
;
// Add the invader to the game (move this outside this class?)
game
.
add
.
existing
(
this
);
};
Invader
.
prototype
=
Object
.
create
(
Phaser
.
Sprite
.
prototype
);
Invader
.
prototype
.
constructor
=
Invader
;
Invader
.
prototype
.
update
=
function
()
{
// Decide if it is time to change direction.
if
(
this
.
game
.
time
.
now
>
this
.
lastTimeChanged
+
DIR_CHANGE_MIN_TIME
)
{
this
.
lastTimeChanged
=
this
.
game
.
time
.
now
;
if
(
this
.
game
.
rnd
.
frac
()
<
this
.
genes
[
'
x_prob_change_dir
'
])
{
this
.
body
.
velocity
.
x
=
-
this
.
body
.
velocity
.
x
;
}
if
(
this
.
game
.
rnd
.
frac
()
<
this
.
genes
[
'
y_prob_change_dir
'
])
{
this
.
body
.
velocity
.
y
=
-
this
.
body
.
velocity
.
y
;
}
}
};
Invader
.
prototype
.
showField
=
function
(
show
,
color
)
{
if
(
show
==
undefined
)
show
=
true
;
if
(
color
==
undefined
)
color
=
0x15AFF0
;
this
.
getChildAt
(
0
).
tilt
=
color
;
this
.
getChildAt
(
0
).
visible
=
show
;
};
Invader
.
prototype
.
freeze
=
function
(
freeze
)
{
if
(
freeze
==
undefined
)
freeze
=
true
;
if
(
freeze
){
this
.
body
.
velocity
.
setTo
(
0
,
0
);
}
else
{
this
.
body
.
velocity
.
x
=
this
.
genes
[
'
xvelocity
'
];
this
.
body
.
velocity
.
y
=
this
.
genes
[
'
yvelocity
'
];
}
};
invadersApp
.
Game
=
function
(
game
)
{
...
...
@@ -139,7 +64,7 @@ invadersApp.Game.prototype = {
// Initialize
this
.
objects
.
invaders
=
[];
for
(
var
i
=
0
;
i
<
INITIAL_INVADERS
;
i
++
)
this
.
objects
.
invaders
.
push
(
new
Invader
(
this
));
for
(
var
i
=
0
;
i
<
INITIAL_INVADERS
;
i
++
)
this
.
objects
.
invaders
.
push
(
new
invadersApp
.
Invader
(
this
));
this
.
player
=
new
invadersApp
.
Player
(
this
);
this
.
game
.
add
.
existing
(
this
.
player
);
...
...
@@ -192,7 +117,6 @@ invadersApp.Game.prototype = {
if
(
alive
>
MIN_INVADERS
){
invader
.
kill
();
}
else
{
// Draw circle?
that
.
objects
.
invaders
.
forEach
(
function
(
invader
)
{
invader
.
showField
(
true
);
});
...
...
index.html
View file @
a6dd0b9c
...
...
@@ -9,6 +9,7 @@
<script
src=
"Preloader.js"
></script>
<script
src=
"js/Utils.js"
></script>
<script
src=
"js/Player.js"
></script>
<script
src=
"js/Invader.js"
></script>
<script
src=
"MainMenu.js"
></script>
<script
src=
"Game.js"
></script>
</head>
...
...
js/Invader.js
0 → 100644
View file @
a6dd0b9c
var
invadersApp
=
invadersApp
||
{};
// Extended sprite object for Invaders
invadersApp
.
Invader
=
function
(
ctx
,
genes
,
x
,
y
)
{
var
game
=
ctx
.
game
;
x
=
x
||
game
.
world
.
randomX
;
y
=
y
||
game
.
world
.
randomY
%
(
game
.
world
.
height
-
WALL_MARGIN
*
1.5
)
;
Phaser
.
Sprite
.
call
(
this
,
game
,
x
,
y
,
'
invader
'
);
game
.
physics
.
enable
(
this
,
Phaser
.
Physics
.
ARCADE
);
// Initialize genes by getting the default values from settings.json
this
.
genes
=
genes
||
function
()
{
var
settings
=
ctx
.
settings
;
var
genes
=
{};
for
(
var
gene
in
settings
.
genes
){
genes
[
gene
]
=
game
.
rnd
.
realInRange
(
settings
.
genes
[
gene
].
min
,
settings
.
genes
[
gene
].
max
);
}
return
genes
;
}();
this
.
anchor
.
setTo
(
0.5
,
0.5
);
var
alpha
=
Math
.
round
(
this
.
genes
[
'
alpha
'
]);
this
.
tint
=
Phaser
.
Color
.
getColor
(
alpha
,
alpha
,
alpha
);
this
.
body
.
velocity
.
x
=
this
.
genes
[
'
xvelocity
'
];
this
.
body
.
velocity
.
y
=
this
.
genes
[
'
yvelocity
'
];
var
scale
=
this
.
genes
[
'
scale
'
];
this
.
scale
.
setTo
(
scale
,
scale
);
this
.
body
.
collideWorldBounds
=
true
;
this
.
body
.
bounce
.
set
(
1
);
// Used to control the probability of x-y change in direction
this
.
lastTimeChanged
=
0
;
// Create a shield
var
shield
=
this
.
game
.
make
.
graphics
(
0
,
0
);
shield
.
lineStyle
(
1
,
0x15AFF0
,
1
);
shield
.
drawCircle
(
-
0.5
,
-
0.5
,
15
*
scale
);
//shield.anchor.setTo(0.5, 0.5);
this
.
addChild
(
shield
);
shield
.
visible
=
false
;
shield
.
scale
.
setTo
(
1.5
/
scale
,
1.5
/
scale
);
// Add the invader to the game (move this outside this class?)
game
.
add
.
existing
(
this
);
};
invadersApp
.
Invader
.
prototype
=
Object
.
create
(
Phaser
.
Sprite
.
prototype
);
invadersApp
.
Invader
.
prototype
.
constructor
=
invadersApp
.
Invader
;
invadersApp
.
Invader
.
prototype
.
update
=
function
()
{
// Decide if it is time to change direction.
if
(
this
.
game
.
time
.
now
>
this
.
lastTimeChanged
+
DIR_CHANGE_MIN_TIME
)
{
this
.
lastTimeChanged
=
this
.
game
.
time
.
now
;
if
(
this
.
game
.
rnd
.
frac
()
<
this
.
genes
[
'
x_prob_change_dir
'
])
{
this
.
body
.
velocity
.
x
=
-
this
.
body
.
velocity
.
x
;
}
if
(
this
.
game
.
rnd
.
frac
()
<
this
.
genes
[
'
y_prob_change_dir
'
])
{
this
.
body
.
velocity
.
y
=
-
this
.
body
.
velocity
.
y
;
}
}
};
invadersApp
.
Invader
.
prototype
.
showField
=
function
(
show
,
color
)
{
if
(
show
==
undefined
)
show
=
true
;
if
(
color
==
undefined
)
color
=
0x15AFF0
;
this
.
getChildAt
(
0
).
tilt
=
color
;
this
.
getChildAt
(
0
).
visible
=
show
;
};
invadersApp
.
Invader
.
prototype
.
freeze
=
function
(
freeze
)
{
if
(
freeze
==
undefined
)
freeze
=
true
;
if
(
freeze
){
this
.
body
.
velocity
.
setTo
(
0
,
0
);
}
else
{
this
.
body
.
velocity
.
x
=
this
.
genes
[
'
xvelocity
'
];
this
.
body
.
velocity
.
y
=
this
.
genes
[
'
yvelocity
'
];
}
};
js/Player.js
View file @
a6dd0b9c
invadersApp
=
invadersApp
||
{};
var
invadersApp
=
invadersApp
||
{};
invadersApp
.
Player
=
function
(
ctx
)
{
invadersApp
.
Player
=
function
(
ctx
,
shootDelay
)
{
this
.
shootDelay
=
shootDelay
;
if
(
this
.
shootDelay
===
undefined
)
{
this
.
shootDelay
=
150
;
}
this
.
ctx
=
ctx
;
this
.
game
=
ctx
.
game
;
...
...
@@ -32,6 +34,8 @@ invadersApp.Player.prototype.constructor = invadersApp.Player;
invadersApp
.
Player
.
prototype
.
update
=
function
()
{
if
(
this
.
game
.
physics
.
arcade
.
isPaused
)
return
;
this
.
body
.
velocity
.
setTo
(
0
,
0
);
if
(
this
.
ctx
.
cursors
.
left
.
isDown
)
{
...
...
@@ -52,7 +56,7 @@ invadersApp.Player.prototype.update = function () {
this
.
readyToFire
=
false
;
// Grab the first bullet we can from the pool
if
(
this
.
game
.
time
.
now
>
this
.
lastShootAt
+
100
)
{
if
(
this
.
game
.
time
.
now
>
this
.
lastShootAt
+
this
.
shootDelay
)
{
this
.
lastShootAt
=
this
.
game
.
time
.
now
;
var
bullet
=
this
.
bullets
.
getFirstExists
(
false
);
if
(
bullet
)
{
...
...
js/Utils.js
View file @
a6dd0b9c
...
...
@@ -6,7 +6,7 @@ invadersApp.utils.TEXT_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!:.©_-/()";
invadersApp
.
utils
.
addText
=
function
(
game
,
x
,
y
,
text
,
size
)
{
var
scale
=
size
||
1
;
var
font
=
game
.
add
.
retroFont
(
'
retroFont
'
,
8
,
8
,
"
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!:.©_-/()
"
,
0
);
var
font
=
game
.
add
.
retroFont
(
'
retroFont
'
,
8
,
8
,
invadersApp
.
utils
.
TEXT_SET
,
0
);
font
.
text
=
text
;
var
img
=
game
.
add
.
image
(
x
,
y
,
font
);
img
.
scale
.
setTo
(
scale
,
scale
);
...
...
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