prkwars 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/prkwars.rb +1 -1
- data/lib/prkwars/bullet.rb +11 -0
- data/lib/prkwars/bullet_turret.rb +9 -0
- data/lib/prkwars/enemy.rb +7 -0
- data/lib/prkwars/explosionparticle.rb +16 -0
- data/lib/prkwars/game.rb +7 -6
- data/lib/prkwars/gameover.rb +10 -0
- data/lib/prkwars/gamespace.rb +20 -1
- data/lib/prkwars/modules.rb +14 -0
- data/lib/prkwars/play.rb +76 -11
- data/lib/prkwars/player.rb +48 -0
- data/lib/prkwars/popuptext.rb +8 -4
- data/lib/prkwars/splash.rb +10 -0
- data/lib/prkwars/stalker.rb +12 -0
- data/lib/prkwars/turret.rb +17 -0
- data/lib/prkwars/warp.rb +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bb20d88899bc28082f190eb177409668b28f7780
|
|
4
|
+
data.tar.gz: bda6b83f8d69d6ecf8a128ed8b8340a276980121
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 62706249a60991fa7c40ae8973dfee889655865cda7ef88c110d2c92e5273c9c2d9484f6685e5961553a72af89e70fa868299640becaad294e523fc1adbf7419
|
|
7
|
+
data.tar.gz: 13011d79b3c81472568f2912e4bc90e1793a636f521bc1b6fd2e7a61d0ce34f8f69c1e7bddad3ae9c62b1bd2c0a4097174acf94f3f65bac3ef7a3b021b5bdf86
|
data/lib/prkwars.rb
CHANGED
|
@@ -2,4 +2,4 @@ PRKWARS_ROOT = File.dirname(File.expand_path(__FILE__))
|
|
|
2
2
|
|
|
3
3
|
require_relative './prkwars/modules'
|
|
4
4
|
Dir[File.join(PRKWARS_ROOT, '/prkwars/*.rb')].each { |f| require f }
|
|
5
|
-
Gosu::Image.autoload_dirs << File.join(File.expand_path('
|
|
5
|
+
Gosu::Image.autoload_dirs << File.join(File.expand_path('../../', __FILE__))
|
data/lib/prkwars/bullet.rb
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
require_relative './modules.rb'
|
|
2
2
|
require 'chingu'
|
|
3
3
|
|
|
4
|
+
##
|
|
4
5
|
# Class representing a bullet shot by the player. Destroys itself
|
|
5
6
|
# if is present out of bounds.
|
|
7
|
+
#
|
|
6
8
|
class Bullet < Chingu::GameObject
|
|
7
9
|
trait :velocity
|
|
8
10
|
trait :bounding_box
|
|
9
11
|
trait :collision_detection
|
|
10
12
|
include GamespacePersistence
|
|
11
13
|
|
|
14
|
+
##
|
|
15
|
+
# Initializes the gamespace where the bullet is present in. Velocity is
|
|
16
|
+
# passed as a paremeter in the options hash
|
|
17
|
+
|
|
12
18
|
def initialize(gamespace, options = {})
|
|
13
19
|
super(options)
|
|
14
20
|
@image = Image['media/bullet.png']
|
|
@@ -17,6 +23,11 @@ class Bullet < Chingu::GameObject
|
|
|
17
23
|
cache_bounding_box
|
|
18
24
|
end
|
|
19
25
|
|
|
26
|
+
##
|
|
27
|
+
# Updating position is not necessary thanks to the velocity trait.
|
|
28
|
+
# In case the bullet is out of bounds, it gets destroyed and explosion
|
|
29
|
+
# particles are spawned.
|
|
30
|
+
|
|
20
31
|
def update
|
|
21
32
|
return if in_bounds(self, @gamespace)
|
|
22
33
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require_relative './modules'
|
|
2
2
|
require 'chingu'
|
|
3
3
|
|
|
4
|
+
##
|
|
4
5
|
# A bullet shot by the Turret enemy unit. Destroys itself if out of bounds.
|
|
5
6
|
class BulletTurret < Chingu::GameObject
|
|
6
7
|
trait :velocity
|
|
@@ -8,12 +9,20 @@ class BulletTurret < Chingu::GameObject
|
|
|
8
9
|
trait :collision_detection
|
|
9
10
|
include GamespacePersistence
|
|
10
11
|
|
|
12
|
+
##
|
|
13
|
+
# Initializes gamespace in which the bullet is present and assigns
|
|
14
|
+
# the turret. Velocity of the bullet is passed as an options parameter,
|
|
15
|
+
# used by the chingu library.
|
|
11
16
|
def initialize(gamespace, options = {})
|
|
12
17
|
super(options)
|
|
13
18
|
@image = Image['media/bullet_turret.png']
|
|
14
19
|
@gamespace = gamespace
|
|
15
20
|
end
|
|
16
21
|
|
|
22
|
+
##
|
|
23
|
+
# As velocity is set by chingu, the only thing update does is that it checks
|
|
24
|
+
# whether there's any reason for the bullets to exist - if not, they get
|
|
25
|
+
# destroyed.
|
|
17
26
|
def update
|
|
18
27
|
destroy! unless in_bounds(self, @gamespace)
|
|
19
28
|
end
|
data/lib/prkwars/enemy.rb
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
require 'chingu'
|
|
2
2
|
|
|
3
|
+
##
|
|
3
4
|
# A generic Enemy class inheriting from Chingu::Gameobject. Any enemy
|
|
4
5
|
# unit inherits from this class. The class contains a method
|
|
5
6
|
# which returns all the descendants - useful for checking all possible
|
|
6
7
|
# collisions.
|
|
8
|
+
#
|
|
7
9
|
class Enemy < Chingu::GameObject
|
|
10
|
+
##
|
|
11
|
+
# Each enemy has hitpoints, how many has to be specified by the enemy class!
|
|
8
12
|
attr_accessor :hp
|
|
9
13
|
trait :bounding_box
|
|
10
14
|
trait :collision_detection
|
|
11
15
|
trait :timer
|
|
12
16
|
|
|
17
|
+
##
|
|
18
|
+
# Method returning all the descendants of the Enemy class. Used for
|
|
19
|
+
# collision checks in the main game loop.
|
|
13
20
|
def self.descendants
|
|
14
21
|
ObjectSpace.each_object(::Class).select { |klass| klass < self }
|
|
15
22
|
end
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
require_relative './modules'
|
|
2
2
|
require 'chingu'
|
|
3
3
|
|
|
4
|
+
##
|
|
4
5
|
# Class used for holding behaviour of particles during explosions of various
|
|
5
6
|
# entitites.
|
|
7
|
+
#
|
|
6
8
|
class ExplosionParticle < Chingu::GameObject
|
|
7
9
|
trait :velocity
|
|
8
10
|
trait :effect # fade
|
|
9
11
|
trait :bounding_box
|
|
10
12
|
include GamespacePersistence
|
|
11
13
|
|
|
14
|
+
##
|
|
15
|
+
# Generates an explosion particle with a set fade_rate (chingu). X and Y
|
|
16
|
+
# velocity is picked randomly between (-7..7).
|
|
17
|
+
|
|
12
18
|
def initialize(gamespace, options = {})
|
|
13
19
|
super(options)
|
|
14
20
|
|
|
@@ -21,6 +27,11 @@ class ExplosionParticle < Chingu::GameObject
|
|
|
21
27
|
@angle = Math.atan2(@velocity_x, @velocity_y) / Math::PI * 180 + 90
|
|
22
28
|
end
|
|
23
29
|
|
|
30
|
+
##
|
|
31
|
+
# In case a particle collides with the gamespace boundaries, velocity
|
|
32
|
+
# gets corrected (impact angle to the other side). A particle gets
|
|
33
|
+
# destroyed once it completely fades out.
|
|
34
|
+
|
|
24
35
|
def update
|
|
25
36
|
velocity_correct! unless bounding_box.collide_rect?(@gamespace.bounding_box)
|
|
26
37
|
destroy! if color.alpha.zero?
|
|
@@ -28,7 +39,9 @@ class ExplosionParticle < Chingu::GameObject
|
|
|
28
39
|
|
|
29
40
|
private
|
|
30
41
|
|
|
42
|
+
##
|
|
31
43
|
# Method correcting the velocity if hitting a wall.
|
|
44
|
+
|
|
32
45
|
def velocity_correct!
|
|
33
46
|
if hitting_side
|
|
34
47
|
@velocity_x = -@velocity_x
|
|
@@ -37,6 +50,9 @@ class ExplosionParticle < Chingu::GameObject
|
|
|
37
50
|
end
|
|
38
51
|
end
|
|
39
52
|
|
|
53
|
+
##
|
|
54
|
+
# Method determining whether a particle is hitting a wall - used to correct
|
|
55
|
+
# the velocity values.
|
|
40
56
|
def hitting_side
|
|
41
57
|
if bounding_box.left < @gamespace.bounding_box.left ||
|
|
42
58
|
bounding_box.right > @gamespace.bounding_box.right
|
data/lib/prkwars/game.rb
CHANGED
|
@@ -2,15 +2,16 @@ require_relative './play'
|
|
|
2
2
|
require 'chingu'
|
|
3
3
|
include Gosu
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
#
|
|
7
|
-
#
|
|
5
|
+
##
|
|
6
|
+
# Game class is simply an encapsulation of the chingu window - essentially, the
|
|
7
|
+
# entry point of the game.
|
|
8
|
+
#
|
|
8
9
|
class Game < Chingu::Window
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
##
|
|
11
|
+
# Initialization of Game only creates a window of a specified size and
|
|
12
|
+
# pushes the beginning game state - +Splash+, into the game state stack.
|
|
11
13
|
def initialize
|
|
12
14
|
super(1280, 720)
|
|
13
|
-
# push_game_state(Play)
|
|
14
15
|
push_game_state(Splash)
|
|
15
16
|
end
|
|
16
17
|
end
|
data/lib/prkwars/gameover.rb
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
require 'chingu'
|
|
2
2
|
|
|
3
|
+
##
|
|
3
4
|
# Class representing the game over state shown when the player loses the game.
|
|
5
|
+
#
|
|
4
6
|
class GameOver < Chingu::GameState
|
|
7
|
+
##
|
|
8
|
+
# Initialization of the game over screen does nothing else but map inputs to
|
|
9
|
+
# the +play+ method (in case the player wishes to play again) and draws text
|
|
10
|
+
# on the screen.
|
|
11
|
+
|
|
5
12
|
def initialize(score, options = {})
|
|
6
13
|
super(options)
|
|
7
14
|
|
|
@@ -12,6 +19,9 @@ class GameOver < Chingu::GameState
|
|
|
12
19
|
x: 20, y: 60)
|
|
13
20
|
end
|
|
14
21
|
|
|
22
|
+
##
|
|
23
|
+
# Switches the game state. Calls +Play.new+ instead of just Play in order
|
|
24
|
+
# to call the initialize method instead of the setup method.
|
|
15
25
|
def play
|
|
16
26
|
switch_game_state(Play.new)
|
|
17
27
|
end
|
data/lib/prkwars/gamespace.rb
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
require 'chingu'
|
|
2
2
|
|
|
3
|
+
##
|
|
3
4
|
# Class representing the space where the game is being played. Class is
|
|
4
5
|
# essentially an encapsulation for the big rectangle where the game can be
|
|
5
6
|
# played. Also holds possible spawn points for enemy entities.
|
|
7
|
+
#
|
|
6
8
|
class GameSpace < Chingu::GameObject
|
|
7
9
|
trait :collision_detection
|
|
8
10
|
trait :bounding_box
|
|
9
11
|
|
|
12
|
+
##
|
|
13
|
+
# Accessor to the player - used by various enemies to determine the position
|
|
14
|
+
# of the player.
|
|
10
15
|
attr_accessor :player
|
|
11
|
-
|
|
16
|
+
|
|
17
|
+
##
|
|
18
|
+
# Initialization method sets up the spawn points for enemies and
|
|
19
|
+
# sets up the background image.
|
|
12
20
|
|
|
13
21
|
def initialize(options = {})
|
|
14
22
|
super(options)
|
|
@@ -17,6 +25,10 @@ class GameSpace < Chingu::GameObject
|
|
|
17
25
|
setup_spawn_points
|
|
18
26
|
end
|
|
19
27
|
|
|
28
|
+
##
|
|
29
|
+
# Returns a random spawn point from the list of spawn points defined
|
|
30
|
+
# by the +setup_spawn_points+ method.
|
|
31
|
+
|
|
20
32
|
def enemy_spawn_point(player_x, player_y)
|
|
21
33
|
sample_points = points_far_enough(player_x, player_y)
|
|
22
34
|
|
|
@@ -25,6 +37,9 @@ class GameSpace < Chingu::GameObject
|
|
|
25
37
|
|
|
26
38
|
private
|
|
27
39
|
|
|
40
|
+
##
|
|
41
|
+
# Sets up a list of possible spawn points to be used by enemies.
|
|
42
|
+
|
|
28
43
|
def setup_spawn_points
|
|
29
44
|
@spawn_points = [[60, 60], [1200, 60], [60, 640], [1200, 640], [800, 640],
|
|
30
45
|
[800, 60], [800, 640], [250, 400], [500, 600],
|
|
@@ -32,6 +47,10 @@ class GameSpace < Chingu::GameObject
|
|
|
32
47
|
[1100, 420]]
|
|
33
48
|
end
|
|
34
49
|
|
|
50
|
+
##
|
|
51
|
+
# Returns a list of points which are far enough from the player. Minimum
|
|
52
|
+
# distance is arbitrary and at the moment set to 130 game units.
|
|
53
|
+
|
|
35
54
|
def points_far_enough(player_x, player_y)
|
|
36
55
|
points = []
|
|
37
56
|
|
data/lib/prkwars/modules.rb
CHANGED
|
@@ -1,22 +1,36 @@
|
|
|
1
1
|
require 'chingu'
|
|
2
2
|
|
|
3
|
+
##
|
|
4
|
+
# Module containing Z order of various game objects.
|
|
3
5
|
module ZOrder
|
|
4
6
|
GAMEOBJECT = 2
|
|
5
7
|
end
|
|
6
8
|
|
|
9
|
+
##
|
|
10
|
+
# Module containing constants used throughout the game.
|
|
7
11
|
module Constants
|
|
8
12
|
PLAYER_VELOCITY = 6
|
|
9
13
|
BULLET_VELOCITY = 12
|
|
10
14
|
end
|
|
11
15
|
|
|
16
|
+
##
|
|
12
17
|
# Module used by classes as a mixin for collision checking with the gamespace
|
|
13
18
|
# so that they do not get out of bounds.
|
|
19
|
+
#
|
|
14
20
|
module GamespacePersistence
|
|
21
|
+
##
|
|
22
|
+
# A method determining whether a gamespace +fully+ contains a certain
|
|
23
|
+
# game object.
|
|
24
|
+
|
|
15
25
|
def in_bounds(entity, gamespace)
|
|
16
26
|
return true if gamespace.bounding_box.contain?(entity.bounding_box)
|
|
17
27
|
false
|
|
18
28
|
end
|
|
19
29
|
|
|
30
|
+
##
|
|
31
|
+
# A method determining game object coordinations in case they are out of
|
|
32
|
+
# bounds.
|
|
33
|
+
|
|
20
34
|
def correct_coords(entity, gamespace)
|
|
21
35
|
if entity.bounding_box.top < gamespace.bounding_box.top # ceiling
|
|
22
36
|
entity.y += gamespace.bounding_box.top - entity.bounding_box.top
|
data/lib/prkwars/play.rb
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
require 'chingu'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
##
|
|
4
|
+
# A class representing the game state of playing the game itself.
|
|
5
|
+
#
|
|
4
6
|
class Play < Chingu::GameState
|
|
5
7
|
trait :timer
|
|
6
8
|
|
|
7
|
-
attr_reader :player, :gamespace
|
|
8
|
-
|
|
9
9
|
PLAYER_START_X = 800
|
|
10
10
|
PLAYER_START_Y = 640
|
|
11
11
|
|
|
12
12
|
include GamespacePersistence
|
|
13
13
|
|
|
14
|
+
##
|
|
15
|
+
# Initialization method of Play maps input to the exit function,
|
|
16
|
+
# creates a +gamespace+ to play in and spawns the +player+.
|
|
17
|
+
# Together with that, sets up variables for enemy generation,
|
|
18
|
+
# score calculation and message displays.
|
|
19
|
+
|
|
14
20
|
def initialize(options = {})
|
|
15
21
|
super(options)
|
|
16
22
|
|
|
17
|
-
self.input = { esc: :exit
|
|
23
|
+
self.input = { esc: :exit }
|
|
18
24
|
|
|
19
25
|
@gamespace = GameSpace.create(x: 1280 / 2, y: 720 / 2)
|
|
20
26
|
@player = Player.create(@gamespace,
|
|
@@ -27,9 +33,11 @@ class Play < Chingu::GameState
|
|
|
27
33
|
setup_display_messages
|
|
28
34
|
end
|
|
29
35
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
36
|
+
##
|
|
37
|
+
# Update loop does three major things each frame:
|
|
38
|
+
# * Collides enemies with player's bullets and reduces their HP if possible.
|
|
39
|
+
# * Collides enemies with player's ship and removes the player's life if so.
|
|
40
|
+
# * Performs another step in the enemy unit generation.
|
|
33
41
|
|
|
34
42
|
def update
|
|
35
43
|
super
|
|
@@ -39,12 +47,11 @@ class Play < Chingu::GameState
|
|
|
39
47
|
generate_random_enemy
|
|
40
48
|
end
|
|
41
49
|
|
|
42
|
-
def draw
|
|
43
|
-
super
|
|
44
|
-
end
|
|
45
|
-
|
|
46
50
|
private
|
|
47
51
|
|
|
52
|
+
##
|
|
53
|
+
# Sets up instance variables for score calculation.
|
|
54
|
+
|
|
48
55
|
def setup_score_calculation
|
|
49
56
|
@score = 0
|
|
50
57
|
@multiplier = 1
|
|
@@ -52,6 +59,11 @@ class Play < Chingu::GameState
|
|
|
52
59
|
@next_multiplier = 2000
|
|
53
60
|
end
|
|
54
61
|
|
|
62
|
+
##
|
|
63
|
+
# Called whenever an enemy dies and the score gets updated.
|
|
64
|
+
# Updates the score as well as the message displayed. Also
|
|
65
|
+
# pops up a +PopupText+ in case that score multiplier goes higher.
|
|
66
|
+
|
|
55
67
|
def update_score
|
|
56
68
|
@score += 100 * @multiplier
|
|
57
69
|
@score_this_life += 100 * @multiplier
|
|
@@ -65,6 +77,13 @@ class Play < Chingu::GameState
|
|
|
65
77
|
@next_multiplier *= 2
|
|
66
78
|
end
|
|
67
79
|
|
|
80
|
+
##
|
|
81
|
+
# A method colliding all the enemies with all the player bullets
|
|
82
|
+
# on the screen. In case an enemy gets hit, +ExplosionParticle+s get
|
|
83
|
+
# spawned and the enemy takes damage. In case the enemy dies, they
|
|
84
|
+
# get destroyed and a +PopupText+ is displayed to show how much
|
|
85
|
+
# score the player got.
|
|
86
|
+
|
|
68
87
|
def collide_enemies_bullets
|
|
69
88
|
Enemy.descendants.each do |enemy|
|
|
70
89
|
enemy.each_bounding_box_collision(Bullet) do |nmy, bullet|
|
|
@@ -84,6 +103,11 @@ class Play < Chingu::GameState
|
|
|
84
103
|
end
|
|
85
104
|
end
|
|
86
105
|
|
|
106
|
+
##
|
|
107
|
+
# A method coliding all the enemies with the player's unit.
|
|
108
|
+
# In case such a collision happens, player unit dies and
|
|
109
|
+
# the +handle_player_death+ method is called.
|
|
110
|
+
|
|
87
111
|
def collide_enemies_player
|
|
88
112
|
Enemy.descendants.each do |enemy|
|
|
89
113
|
@player.each_bounding_box_collision(enemy) do
|
|
@@ -97,6 +121,9 @@ class Play < Chingu::GameState
|
|
|
97
121
|
end
|
|
98
122
|
end
|
|
99
123
|
|
|
124
|
+
##
|
|
125
|
+
# A method generating random enemies. Steps up the framecounter
|
|
126
|
+
# for each enemy type by one and calls their corresponding methods.
|
|
100
127
|
def generate_random_enemy
|
|
101
128
|
@framecounter_turret += 1
|
|
102
129
|
generate_random_turret
|
|
@@ -105,6 +132,11 @@ class Play < Chingu::GameState
|
|
|
105
132
|
generate_random_stalker
|
|
106
133
|
end
|
|
107
134
|
|
|
135
|
+
##
|
|
136
|
+
# Generates a +Turret+ in a random position if the framecounter got
|
|
137
|
+
# to the next generation point. If so, the framecounter gets reset
|
|
138
|
+
# to zero.
|
|
139
|
+
|
|
108
140
|
def generate_random_turret
|
|
109
141
|
return if @framecounter_turret != @nextgen_turret
|
|
110
142
|
|
|
@@ -119,6 +151,11 @@ class Play < Chingu::GameState
|
|
|
119
151
|
@nextgen_turret = rand(80..150)
|
|
120
152
|
end
|
|
121
153
|
|
|
154
|
+
##
|
|
155
|
+
# Generates a +Stalker+ in a random position if the framecounter got
|
|
156
|
+
# to the next generation point. If so, the framecounter gets reset
|
|
157
|
+
# to zero.
|
|
158
|
+
|
|
122
159
|
def generate_random_stalker
|
|
123
160
|
return if @framecounter_stalker != @nextgen_stalker
|
|
124
161
|
|
|
@@ -130,10 +167,16 @@ class Play < Chingu::GameState
|
|
|
130
167
|
@nextgen_stalker = rand(20..50)
|
|
131
168
|
end
|
|
132
169
|
|
|
170
|
+
##
|
|
171
|
+
# Method spawning a +Warp+ at a point provided by the parameters..
|
|
172
|
+
|
|
133
173
|
def create_warp(x, y)
|
|
134
174
|
Warp.create(zorder: ZOrder::GAMEOBJECT, x: x, y: y)
|
|
135
175
|
end
|
|
136
176
|
|
|
177
|
+
##
|
|
178
|
+
# Method generating a random position for an enemy unit to spawn in.
|
|
179
|
+
|
|
137
180
|
def generate_random_position
|
|
138
181
|
x, y = @gamespace.enemy_spawn_point(@player.x, @player.y)
|
|
139
182
|
|
|
@@ -143,10 +186,20 @@ class Play < Chingu::GameState
|
|
|
143
186
|
[x, y]
|
|
144
187
|
end
|
|
145
188
|
|
|
189
|
+
##
|
|
190
|
+
# Method switching to the +GameOver+ state.
|
|
191
|
+
|
|
146
192
|
def handle_game_over
|
|
147
193
|
switch_game_state(GameOver.new(@score))
|
|
148
194
|
end
|
|
149
195
|
|
|
196
|
+
##
|
|
197
|
+
# Method handling the death of a player.
|
|
198
|
+
# Kills all the units, reduces player's lives by one and goes to
|
|
199
|
+
# +GameOver+ if player's lives were reduced to zero.
|
|
200
|
+
# If not, +ExplosionParticle+s get spawned, all enemy generation
|
|
201
|
+
# is reset and the player starts from a new point.
|
|
202
|
+
|
|
150
203
|
def handle_player_death
|
|
151
204
|
kill_everything
|
|
152
205
|
@player.lives -= 1
|
|
@@ -168,6 +221,9 @@ class Play < Chingu::GameState
|
|
|
168
221
|
@player.y = PLAYER_START_Y
|
|
169
222
|
end
|
|
170
223
|
|
|
224
|
+
##
|
|
225
|
+
# Method killing all game objects except for the player's unit.
|
|
226
|
+
|
|
171
227
|
def kill_everything
|
|
172
228
|
Enemy.descendants.each do |enemy|
|
|
173
229
|
ObjectSpace.each_object(enemy, &:destroy!)
|
|
@@ -178,6 +234,11 @@ class Play < Chingu::GameState
|
|
|
178
234
|
ObjectSpace.each_object(Bullet, &:destroy!)
|
|
179
235
|
end
|
|
180
236
|
|
|
237
|
+
##
|
|
238
|
+
# Method setting up framecounters for each generation,
|
|
239
|
+
# generates a random number for the next generation of
|
|
240
|
+
# enemy units.
|
|
241
|
+
|
|
181
242
|
def setup_enemy_generation
|
|
182
243
|
@framecounter_turret = 0
|
|
183
244
|
@nextgen_turret = rand(150..300)
|
|
@@ -185,6 +246,10 @@ class Play < Chingu::GameState
|
|
|
185
246
|
@nextgen_stalker = rand(110..180)
|
|
186
247
|
end
|
|
187
248
|
|
|
249
|
+
##
|
|
250
|
+
# Method setting up the messages displayed in the HUD
|
|
251
|
+
# to show the amount of player's lives and the player's score.
|
|
252
|
+
|
|
188
253
|
def setup_display_messages
|
|
189
254
|
@message_lives = Chingu::Text.create("Lives: #{@player.lives}",
|
|
190
255
|
x: 1020, y: 5, size: 30)
|
data/lib/prkwars/player.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require_relative './modules'
|
|
2
2
|
require 'chingu'
|
|
3
3
|
|
|
4
|
+
##
|
|
4
5
|
# Class holding all the logic related to the player - input controller
|
|
5
6
|
# and any manipulation of player's sprite.
|
|
6
7
|
class Player < Chingu::GameObject
|
|
@@ -16,6 +17,9 @@ class Player < Chingu::GameObject
|
|
|
16
17
|
STARTING_BOMBS = 3
|
|
17
18
|
INVUL_FRAMES = 30
|
|
18
19
|
|
|
20
|
+
##
|
|
21
|
+
# Initialization of player assigns gamespace to the player, their sprite
|
|
22
|
+
# and sets the input layout for the player.
|
|
19
23
|
def initialize(gamespace, options = {})
|
|
20
24
|
super(options)
|
|
21
25
|
|
|
@@ -31,42 +35,74 @@ class Player < Chingu::GameObject
|
|
|
31
35
|
@bombs = STARTING_BOMBS
|
|
32
36
|
end
|
|
33
37
|
|
|
38
|
+
##
|
|
39
|
+
# A method changing the player's y position by the +PLAYER_VELOCITY+
|
|
40
|
+
# constant.
|
|
41
|
+
|
|
34
42
|
def move_up
|
|
35
43
|
@y -= Constants::PLAYER_VELOCITY
|
|
36
44
|
@y_change = -Constants::PLAYER_VELOCITY
|
|
37
45
|
end
|
|
38
46
|
|
|
47
|
+
##
|
|
48
|
+
# A method changing the player's y position by the +PLAYER_VELOCITY+
|
|
49
|
+
# constant.
|
|
50
|
+
|
|
39
51
|
def move_down
|
|
40
52
|
@y += Constants::PLAYER_VELOCITY
|
|
41
53
|
@y_change = Constants::PLAYER_VELOCITY
|
|
42
54
|
end
|
|
43
55
|
|
|
56
|
+
##
|
|
57
|
+
# A method changing the player's x position by the +PLAYER_VELOCITY+
|
|
58
|
+
# constant.
|
|
59
|
+
|
|
44
60
|
def move_left
|
|
45
61
|
@x -= Constants::PLAYER_VELOCITY
|
|
46
62
|
@x_change = -Constants::PLAYER_VELOCITY
|
|
47
63
|
end
|
|
48
64
|
|
|
65
|
+
##
|
|
66
|
+
# A method changing the player's x position by the +PLAYER_VELOCITY+
|
|
67
|
+
# constant.
|
|
68
|
+
|
|
49
69
|
def move_right
|
|
50
70
|
@x += Constants::PLAYER_VELOCITY
|
|
51
71
|
@x_change = Constants::PLAYER_VELOCITY
|
|
52
72
|
end
|
|
53
73
|
|
|
74
|
+
##
|
|
75
|
+
# Sets the shooting vector's x part to +-BULLET_VELOCITY+.
|
|
76
|
+
|
|
54
77
|
def shoot_left
|
|
55
78
|
@shoot_vec_x = -Constants::BULLET_VELOCITY
|
|
56
79
|
end
|
|
57
80
|
|
|
81
|
+
##
|
|
82
|
+
# Sets the shooting vector's x part to +BULLET_VELOCITY+.
|
|
83
|
+
|
|
58
84
|
def shoot_right
|
|
59
85
|
@shoot_vec_x = Constants::BULLET_VELOCITY
|
|
60
86
|
end
|
|
61
87
|
|
|
88
|
+
##
|
|
89
|
+
# Sets the shooting vector's y part to +-BULLET_VELOCITY+.
|
|
90
|
+
|
|
62
91
|
def shoot_up
|
|
63
92
|
@shoot_vec_y = -Constants::BULLET_VELOCITY
|
|
64
93
|
end
|
|
65
94
|
|
|
95
|
+
##
|
|
96
|
+
# Sets the shooting vector's y part to +BULLET_VELOCITY+.
|
|
66
97
|
def shoot_down
|
|
67
98
|
@shoot_vec_y = Constants::BULLET_VELOCITY
|
|
68
99
|
end
|
|
69
100
|
|
|
101
|
+
##
|
|
102
|
+
# Method updating the player each frame. Player's sprite gets rotated
|
|
103
|
+
# to correspond to his movement direction. In case they're out of bounds
|
|
104
|
+
# the player's coordinates are corrected and bullets are shot if possible.
|
|
105
|
+
|
|
70
106
|
def update
|
|
71
107
|
rotate_player
|
|
72
108
|
correct_coords(self, @gamespace) unless in_bounds(self, @gamespace)
|
|
@@ -79,6 +115,10 @@ class Player < Chingu::GameObject
|
|
|
79
115
|
|
|
80
116
|
private
|
|
81
117
|
|
|
118
|
+
##
|
|
119
|
+
# Initializes variables used by the player class to determine shooting
|
|
120
|
+
# speed as well as rotations of their sprite.
|
|
121
|
+
|
|
82
122
|
def init_vector_variables
|
|
83
123
|
@x_change = 0 # Used for rotating the triangular sprite
|
|
84
124
|
@y_change = 0
|
|
@@ -87,6 +127,9 @@ class Player < Chingu::GameObject
|
|
|
87
127
|
@shoot_vec_y = 0
|
|
88
128
|
end
|
|
89
129
|
|
|
130
|
+
##
|
|
131
|
+
# Rotates the player sprite if there was a change in their movement direction.
|
|
132
|
+
|
|
90
133
|
def rotate_player
|
|
91
134
|
return unless @x_change != 0 || @y_change != 0
|
|
92
135
|
|
|
@@ -98,6 +141,11 @@ class Player < Chingu::GameObject
|
|
|
98
141
|
@y_change = 0
|
|
99
142
|
end
|
|
100
143
|
|
|
144
|
+
##
|
|
145
|
+
# Method shooting bullets in the direction which was input by the player.
|
|
146
|
+
# These directions are repesented by the @shoot_vec_x and @shoot_vec_y
|
|
147
|
+
# instance variables.
|
|
148
|
+
|
|
101
149
|
def shoot_bullet
|
|
102
150
|
return unless @shoot_vec_x != 0 || @shoot_vec_y != 0
|
|
103
151
|
|
data/lib/prkwars/popuptext.rb
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
require 'chingu'
|
|
2
2
|
include Gosu
|
|
3
3
|
|
|
4
|
+
##
|
|
4
5
|
# Class used for messages that pop up on the screen whenever something
|
|
5
6
|
# relevant happens - e.g. score goes up.
|
|
7
|
+
#
|
|
6
8
|
class PopupText < Chingu::GameObject
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
##
|
|
10
|
+
# Initialization method for +PopupText+ requires x, y to determine
|
|
11
|
+
# where the message gets spawned on the screen.
|
|
9
12
|
|
|
10
13
|
def initialize(message, x, y, options = {})
|
|
11
14
|
super(options)
|
|
12
15
|
|
|
13
|
-
@velocity_x = 0
|
|
14
|
-
@velocity_y = -2
|
|
15
16
|
@msg = Chingu::Text.create(message, x: x, y: y, size: 30)
|
|
16
17
|
end
|
|
17
18
|
|
|
19
|
+
##
|
|
20
|
+
# Updating the popuptext changes the y coordinate and slowly fades
|
|
21
|
+
# the message out.
|
|
18
22
|
def update
|
|
19
23
|
@msg.y -= 1
|
|
20
24
|
@msg.color.alpha -= 4
|
data/lib/prkwars/splash.rb
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
require 'chingu'
|
|
2
2
|
|
|
3
|
+
##
|
|
3
4
|
# Class representing the game state right after turning the game on.
|
|
5
|
+
|
|
4
6
|
class Splash < Chingu::GameState
|
|
7
|
+
##
|
|
8
|
+
# A setup method called by the chingu library when the +Splash+ class
|
|
9
|
+
# gets pushed into the game state stack/gets switched to.
|
|
10
|
+
# Sets up input for exit/playing the game and sets up splash messages.
|
|
5
11
|
def setup
|
|
6
12
|
self.input = { space: :play, esc: :exit }
|
|
7
13
|
Chingu::Text.create('PRK WARS',
|
|
@@ -15,6 +21,10 @@ class Splash < Chingu::GameState
|
|
|
15
21
|
x: 20, y: 170, size: 30)
|
|
16
22
|
end
|
|
17
23
|
|
|
24
|
+
##
|
|
25
|
+
# A method called when the player presses space in order to play the game.
|
|
26
|
+
# Uses the +transitional_game_state+ method provided by the +chingu+ library
|
|
27
|
+
# in order to make things prettier.
|
|
18
28
|
def play
|
|
19
29
|
transitional_game_state(Chingu::GameStates::FadeTo, speed: 10)
|
|
20
30
|
switch_game_state(Play.new)
|
data/lib/prkwars/stalker.rb
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
require_relative './enemy'
|
|
2
2
|
require 'chingu'
|
|
3
3
|
|
|
4
|
+
##
|
|
4
5
|
# Class representing the Stalker enemy unit which is mobile but unable to
|
|
5
6
|
# shoot and endlessly moves towards the player unit.
|
|
7
|
+
#
|
|
6
8
|
class Stalker < Enemy
|
|
7
9
|
trait :velocity
|
|
8
10
|
include GamespacePersistence
|
|
9
11
|
VELOCITY = 2
|
|
10
12
|
HP = 1
|
|
11
13
|
|
|
14
|
+
##
|
|
15
|
+
# Initialization of the Stalker unit requires the gamespace for collision
|
|
16
|
+
# checks, sets the sprite and HP of the unit to an instance variable.
|
|
17
|
+
|
|
12
18
|
def initialize(gamespace, options = {})
|
|
13
19
|
super(options)
|
|
14
20
|
|
|
@@ -18,6 +24,10 @@ class Stalker < Enemy
|
|
|
18
24
|
@hp = HP
|
|
19
25
|
end
|
|
20
26
|
|
|
27
|
+
##
|
|
28
|
+
# Every time a Stalker unit gets updated, they move in a constant speed
|
|
29
|
+
# towards the player. Their sprite also gets rotated.
|
|
30
|
+
|
|
21
31
|
def update
|
|
22
32
|
dist_x = @gamespace.player.x - @x
|
|
23
33
|
dist_y = @gamespace.player.y - @y
|
|
@@ -33,6 +43,8 @@ class Stalker < Enemy
|
|
|
33
43
|
@angle = rot_to
|
|
34
44
|
end
|
|
35
45
|
|
|
46
|
+
##
|
|
47
|
+
# A common method to enemies, reducing their HP by one.
|
|
36
48
|
def take_damage
|
|
37
49
|
@hp -= 1
|
|
38
50
|
end
|
data/lib/prkwars/turret.rb
CHANGED
|
@@ -3,13 +3,21 @@ require_relative './enemy'
|
|
|
3
3
|
require_relative './bullet_turret'
|
|
4
4
|
require 'chingu'
|
|
5
5
|
|
|
6
|
+
##
|
|
6
7
|
# Class representing the Turret enemy unit which is stationary and endlessly
|
|
7
8
|
# shoots bullets in the direction of the player.
|
|
9
|
+
#
|
|
8
10
|
class Turret < Enemy
|
|
9
11
|
SHOOT_GAP = 40
|
|
10
12
|
VELOCITY = 3
|
|
11
13
|
HP = 3
|
|
12
14
|
|
|
15
|
+
##
|
|
16
|
+
# Initializing a +Turret+ builds a new +Chingu::Animation+ with the
|
|
17
|
+
# turret sprites. Each time a +Turret+ gets hit, it changes its sprite
|
|
18
|
+
# to better display the amount of HP. Also sets the gamespace instance variable,
|
|
19
|
+
# HP and a shooting timer.
|
|
20
|
+
|
|
13
21
|
def initialize(gamespace, options = {})
|
|
14
22
|
super(options)
|
|
15
23
|
|
|
@@ -21,6 +29,10 @@ class Turret < Enemy
|
|
|
21
29
|
@image = @animation[@hp - 1]
|
|
22
30
|
end
|
|
23
31
|
|
|
32
|
+
##
|
|
33
|
+
# Updating a +Turret+ only consists of shooting a bullet if the framecounter
|
|
34
|
+
# got far enough.
|
|
35
|
+
|
|
24
36
|
def update
|
|
25
37
|
@shoot_timer += 1
|
|
26
38
|
return unless @shoot_timer == SHOOT_GAP
|
|
@@ -29,6 +41,9 @@ class Turret < Enemy
|
|
|
29
41
|
@shoot_timer = 0
|
|
30
42
|
end
|
|
31
43
|
|
|
44
|
+
##
|
|
45
|
+
# A common +Enemy+ method - reduces the unit's HP by one and changes the
|
|
46
|
+
# sprite.
|
|
32
47
|
def take_damage
|
|
33
48
|
@hp -= 1
|
|
34
49
|
@image = @animation[@hp - 1]
|
|
@@ -36,6 +51,8 @@ class Turret < Enemy
|
|
|
36
51
|
|
|
37
52
|
private
|
|
38
53
|
|
|
54
|
+
##
|
|
55
|
+
# Shoots a bullet at a proper velocity towards the player's unit.
|
|
39
56
|
def shoot_bullet
|
|
40
57
|
dist_x = @gamespace.player.x - @x
|
|
41
58
|
dist_y = @gamespace.player.y - @y
|
data/lib/prkwars/warp.rb
CHANGED
|
@@ -5,6 +5,8 @@ require 'chingu'
|
|
|
5
5
|
class Warp < Chingu::GameObject
|
|
6
6
|
trait :effect # fade
|
|
7
7
|
|
|
8
|
+
##
|
|
9
|
+
# Sets the image of a warp as well as its fade rate.
|
|
8
10
|
def initialize(options = {})
|
|
9
11
|
super(options)
|
|
10
12
|
|
|
@@ -12,6 +14,8 @@ class Warp < Chingu::GameObject
|
|
|
12
14
|
@image = Image['media/warp.png']
|
|
13
15
|
end
|
|
14
16
|
|
|
17
|
+
##
|
|
18
|
+
# Destroys a +Warp+ if it faded out.
|
|
15
19
|
def update
|
|
16
20
|
destroy! if color.alpha.zero?
|
|
17
21
|
end
|