/
spec_helper.rb
166 lines (130 loc) · 3.56 KB
/
spec_helper.rb
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
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'config'))
require 'environment'
require GAMEBOX_PATH + 'spec/helper'
module KillboxAcceptanceHelpers
def configure_game_with_testing_stage(opts={})
opts[:tileset] ||= "tileset" # will look for a png in spec/fixtures/graphics/map/
opts[:tile_size] ||= 36 # 36x36 is the size of tiles in tileset.png, but this can be changed for testing purposes
opts[:player_count] ||= 1
Gamebox.configuration.stages = [:level_play]
Stage.definitions[:level_play].curtain_up do
extend TestStageHelpers
director.update_slots = [:first, :before, :update, :last]
tmx_map = KillboxAcceptanceHelpers.get_test_map(opts[:map_name])
map_data = LevelLoader::MapData.new
map_data.tile_grid = LevelLoader.generate_map(tmx_map)[0]
map_data.tileset_image = "map/#{opts[:tileset]}.png"
map_data.tile_size = opts[:tile_size]
@level = LevelLoader::Level.new
@level.map = self.create_actor :map, map_data: map_data
@level.map_extents = [0,0, map_data.tile_grid[0].size * map_data.tile_size, map_data.tile_grid.size * map_data.tile_size]
LevelLoader.load_objects self, tmx_map, @level
setup_players opts[:player_count]
end
game
end
def body_vector(body)
vec2(body.x,body.y)
end
def get_test_map(name)
Tmx.load("#{APP_ROOT}/spec/fixtures/maps/#{name}.tmx")
end
module_function :get_test_map
def jump(time_held)
# charge & jump
hold_key KbN, time_held, step: 20
end
def throw_bomb(time_held=40)
# charge & throw
hold_key KbM, time_held, step: 10
update 20
end
def max_jump
jump 2_000
end
def charge_and_throw_bomb(time_held)
hold_key KbM, time_held, step: 20
end
def walk_left(time_held, &blk)
hold_key KbA, time_held, step: 20, &blk
end
def walk_right(time_held, &blk)
hold_key KbD, time_held, step: 20, &blk
end
def hold_key(key, time_held, opts={}, &blk)
press_key key
yield if block_given?
update time_held, step: opts[:step]
release_key key
end
def look_up
tap_key KbW
end
def look_down
tap_key KbS
end
def look_right
tap_key KbD
end
def look_left
tap_key KbA
end
def shields_up
tap_key KbV
end
def shoot
tap_key KbB
end
def tap_key(key)
press_key key
release_key key
end
def place_land_mine
press_key KbS # look down
hold_key KbM, 10 # set land mine
update 1
release_key KbS
end
def see_bottom_right_standing_above(y)
player.collision_points[4].y.should == (y - 1).ish
end
def see_bottom_left_standing_above(y)
player.collision_points[5].y.should == (y - 1).ish
end
def see_player_within_zone(zone)
zone_bb = Rect.new(zone.x, zone.y, zone.width, zone.height)
# TODO better helper to show the bounding boxes in question
zone_bb.contain?(player.bb).should be_true
end
end
RSpec.configure do |config|
config.mock_with :mocha
config.include KillboxAcceptanceHelpers
end
class Numeric
def ish(acceptable_delta=0.001)
ApproximateValue.new self, acceptable_delta
end
end
module Enumerable
def ish(acceptable_delta=0.001)
self.map { |item| ApproximateValue.new item, acceptable_delta }
end
end
class ApproximateValue
def initialize(me, acceptable_delta)
@me = me
@acceptable_delta = acceptable_delta
end
def ==(other)
(other - @me).abs < @acceptable_delta
end
def to_s
"within #{@acceptable_delta} of #{@me}"
end
alias inspect to_s
end
# class MockImage
# def width; 26; end
# def height; 30; end
# end