rubots 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rubots/game.rb +11 -6
- data/lib/rubots/robot.rb +18 -10
- data/lib/rubots/strategy_loader.rb +13 -2
- metadata +2 -2
data/lib/rubots/game.rb
CHANGED
@@ -3,6 +3,7 @@ module Rubots
|
|
3
3
|
attr_reader :robots, :laser_beams
|
4
4
|
MAP_HEIGHT = 700
|
5
5
|
MAP_WIDTH = 1000
|
6
|
+
CELL_SIZE = 40 # Positioning cell
|
6
7
|
|
7
8
|
def initialize(robots)
|
8
9
|
@robots = robots.map { |klass| Robot.new(klass, self, *random_location) }
|
@@ -10,8 +11,8 @@ module Rubots
|
|
10
11
|
|
11
12
|
def tick
|
12
13
|
@laser_beams = []
|
13
|
-
@robots.each { |robot| robot.
|
14
|
-
@robots.each { |robot| robot.
|
14
|
+
@robots.each { |robot| robot.process_command }
|
15
|
+
@robots.each { |robot| robot.tick }
|
15
16
|
check_collisions
|
16
17
|
check_out_of_area
|
17
18
|
check_beam_hits
|
@@ -38,11 +39,15 @@ module Rubots
|
|
38
39
|
|
39
40
|
private
|
40
41
|
|
41
|
-
# TODO enforce separation
|
42
42
|
def random_location
|
43
|
-
|
44
|
-
|
45
|
-
[
|
43
|
+
x_cells = MAP_WIDTH / CELL_SIZE
|
44
|
+
y_cells = MAP_HEIGHT / CELL_SIZE
|
45
|
+
@taken ||= []
|
46
|
+
begin
|
47
|
+
pair = [rand(x_cells), rand(y_cells)]
|
48
|
+
end while puts("coll") || @taken.include?(pair)
|
49
|
+
@taken << pair
|
50
|
+
pair.map { |coord| coord * CELL_SIZE + CELL_SIZE / 2 }
|
46
51
|
end
|
47
52
|
|
48
53
|
COLLISION_DISTANCE = 32
|
data/lib/rubots/robot.rb
CHANGED
@@ -33,7 +33,7 @@ module Rubots
|
|
33
33
|
@cooldown_timer = 0
|
34
34
|
end
|
35
35
|
|
36
|
-
def
|
36
|
+
def process_command
|
37
37
|
return if @destroyed
|
38
38
|
|
39
39
|
if @cooldown_timer > 0
|
@@ -43,18 +43,16 @@ module Rubots
|
|
43
43
|
command.apply_to(self)
|
44
44
|
@cooldown_timer = command.cooldown
|
45
45
|
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def tick
|
49
|
+
return if @destroyed
|
50
|
+
|
46
51
|
tick_angle
|
47
52
|
tick_throttle
|
48
53
|
tick_movement
|
49
54
|
tick_gun
|
50
|
-
|
51
|
-
|
52
|
-
# It's a separate method because we want fire to be after *every* robot moved
|
53
|
-
def tick_fire
|
54
|
-
if @firing
|
55
|
-
@game.laser_fire(Beam.from(self))
|
56
|
-
@firing = false
|
57
|
-
end
|
55
|
+
tick_fire
|
58
56
|
end
|
59
57
|
|
60
58
|
def name
|
@@ -87,7 +85,10 @@ module Rubots
|
|
87
85
|
end
|
88
86
|
|
89
87
|
def targets_data
|
90
|
-
@game.robots.
|
88
|
+
robots_arrays = @game.robots.slice_before(self).to_a
|
89
|
+
robots_arrays.last.shift # First is self
|
90
|
+
robots = robots_arrays.reverse.inject(&:+)
|
91
|
+
robots.map do |target_robot|
|
91
92
|
next if target_robot == self
|
92
93
|
x_dist = @x - target_robot.x
|
93
94
|
y_dist = @y - target_robot.y
|
@@ -148,5 +149,12 @@ module Rubots
|
|
148
149
|
end
|
149
150
|
end
|
150
151
|
end
|
152
|
+
|
153
|
+
def tick_fire
|
154
|
+
if @firing
|
155
|
+
@game.laser_fire(Beam.from(self))
|
156
|
+
@firing = false
|
157
|
+
end
|
158
|
+
end
|
151
159
|
end
|
152
160
|
end
|
@@ -3,7 +3,7 @@ module Rubots
|
|
3
3
|
def self.load(params)
|
4
4
|
return default_lineup unless params.any?
|
5
5
|
|
6
|
-
params.map { |p| new(p).strategy_class }
|
6
|
+
params.map { |p| new(p).strategy_class }.flatten
|
7
7
|
end
|
8
8
|
|
9
9
|
def self.default_lineup
|
@@ -16,7 +16,9 @@ module Rubots
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def strategy_class
|
19
|
-
if
|
19
|
+
if is_multiple?
|
20
|
+
multiple_classes
|
21
|
+
elsif is_sample?
|
20
22
|
sample_class
|
21
23
|
else
|
22
24
|
class_from_file
|
@@ -25,6 +27,15 @@ module Rubots
|
|
25
27
|
|
26
28
|
private
|
27
29
|
|
30
|
+
def is_multiple?
|
31
|
+
@name.match(/^[0-9]+\*/)
|
32
|
+
end
|
33
|
+
|
34
|
+
def multiple_classes
|
35
|
+
parts = @name.split('*', 2)
|
36
|
+
[StrategyLoader.new(parts[1]).strategy_class] * parts[0].to_i
|
37
|
+
end
|
38
|
+
|
28
39
|
def class_from_file
|
29
40
|
load @name
|
30
41
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubots
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-06-
|
12
|
+
date: 2014-06-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: gosu
|