gembots 0.0.03 → 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gembots/arena.rb +21 -36
- data/lib/gembots/bot.rb +38 -92
- data/test/test_arena.rb +16 -11
- metadata +17 -5
- data/test/test_bot.rb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e7ee5cc5f03ca3b06cda8560005f4be2a60bbe8
|
4
|
+
data.tar.gz: 1272a7b540898904aafe8c4635157fbdb7c3b4a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51c394cac319498bc612fb43b9639e88fee49ca978c92ac372d9792c3a8894e4767588683407ab3eaee6346faf3d01aae7171a9cace938291d63fe03cb1e0c4d
|
7
|
+
data.tar.gz: 1e79f3722a61bbd7cbdcacb25444ba2b638d26b989642d62f1c494b382479b9236c4dc2d7ca63c57709e2a08c5d5aa74e0d65ea25cf1a1238b031de86bb32df6
|
data/lib/gembots/arena.rb
CHANGED
@@ -1,48 +1,33 @@
|
|
1
1
|
require 'gembots'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
# This is a hash table containing the ids of each object.
|
6
|
-
attr_reader :objects
|
7
|
-
|
8
|
-
# Same as objects, but it contains and older copy to detect changes during a update_bot.
|
9
|
-
attr_reader :objects_pre
|
10
|
-
|
11
|
-
# 2-dimensional array of the current arena.
|
12
|
-
# By default this is a 20x20 board
|
13
|
-
attr_reader :board
|
14
|
-
|
3
|
+
class Gembots::Arena < Gosu::Window
|
4
|
+
attr_reader :bots
|
15
5
|
def initialize *bots
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
bots.each do |
|
21
|
-
bot
|
22
|
-
|
23
|
-
@
|
24
|
-
|
25
|
-
def bot.update arena, x_old = nil, y_old = nil
|
26
|
-
arena.update_bot self, x_old, y_old
|
27
|
-
end
|
6
|
+
super 640, 480, false
|
7
|
+
self.caption = "Gembots battle"
|
8
|
+
@bots = []
|
9
|
+
|
10
|
+
bots.each do |bot_class|
|
11
|
+
bot = bot_class.new self
|
12
|
+
bot.warp 320, 240
|
13
|
+
@bots << bot
|
28
14
|
end
|
29
15
|
end
|
30
16
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
# remove id from board
|
35
|
-
@board[x_old][y_old].delete robot.id
|
17
|
+
def draw
|
18
|
+
@bots.each &:draw
|
19
|
+
end
|
36
20
|
|
37
|
-
|
38
|
-
|
21
|
+
def update
|
22
|
+
@bots.each &:update
|
23
|
+
@bots.each do |bot|
|
24
|
+
if bot.actions.empty?
|
25
|
+
bot.on_idle
|
26
|
+
end
|
39
27
|
end
|
40
28
|
end
|
41
29
|
|
42
|
-
|
43
|
-
|
44
|
-
def spawn object
|
45
|
-
@objects[object.id] = object
|
46
|
-
@board[object.x_pos][object.y_pos] << object.id
|
30
|
+
def button_down id
|
31
|
+
close if id == Gosu::KbEscape
|
47
32
|
end
|
48
33
|
end
|
data/lib/gembots/bot.rb
CHANGED
@@ -1,113 +1,59 @@
|
|
1
1
|
require 'gembots'
|
2
2
|
|
3
|
-
# The Robot class is used to create and define robots.
|
4
3
|
class Gembots::Robot
|
5
|
-
|
6
|
-
# This will typically used for things like announcements during battles.
|
7
|
-
attr_accessor :name
|
4
|
+
attr_reader :x, :y, :angle, :actions
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
# Number between 0 and 360 representing the robot's current facing angle.
|
16
|
-
attr_reader :angle
|
17
|
-
|
18
|
-
# This is simply the robot's player ID.
|
19
|
-
# It is typically the Object ID, but may be changed to fit other uses.
|
20
|
-
attr_reader :id
|
21
|
-
|
22
|
-
# This is the ID of a cloned robot's parent.
|
23
|
-
# Otherwise if the robot is not a cloned one, the value will be nil.
|
24
|
-
attr_accessor :parent_id
|
25
|
-
|
26
|
-
# This is set and used by the current robot's arena, if there is one.
|
27
|
-
attr_accessor :arena
|
28
|
-
|
29
|
-
def initialize name = 'Robot'
|
30
|
-
@name = name
|
31
|
-
|
32
|
-
@x_pos = 0
|
33
|
-
@y_pos = 0
|
34
|
-
@angle = 0
|
35
|
-
|
36
|
-
@id = self.object_id
|
37
|
-
@parent_id = nil
|
6
|
+
def initialize window
|
7
|
+
@actions = []
|
8
|
+
@window = window
|
9
|
+
@images = Gosu::Image::load_tiles(window, "media/tank.png", 32, 32, false)
|
10
|
+
@image = Gosu::Image.new window, "media/cannon.png", false
|
11
|
+
@x = @y = @angle = @cur_image = 0.0
|
38
12
|
end
|
39
13
|
|
40
|
-
|
41
|
-
|
42
|
-
clone = self.dup
|
43
|
-
clone.parent_id = self.id
|
44
|
-
clone
|
14
|
+
def warp x, y
|
15
|
+
@x, @y = x, y
|
45
16
|
end
|
46
17
|
|
47
|
-
|
48
|
-
|
49
|
-
@parent_id == target_robot.id
|
18
|
+
def turn angle=10
|
19
|
+
@actions << [:turn, angle]
|
50
20
|
end
|
51
21
|
|
52
|
-
|
53
|
-
|
54
|
-
# Currently it only supports movement along 8 directions.
|
55
|
-
def move dist = 1
|
56
|
-
y_old, x_old = @y_pos, @x_pos
|
57
|
-
# Eventually some math using rotation_matrix will be here, in order to calculate all 360 directions.
|
58
|
-
# For now I'm only implementing 8 directions.
|
59
|
-
directions = [
|
60
|
-
[1, 0], # 0
|
61
|
-
[1, 1], # 45
|
62
|
-
[0, 1], # 90
|
63
|
-
[-1, 1], # 135
|
64
|
-
[-1, 0], # 180
|
65
|
-
[-1, -1], # 225
|
66
|
-
[0, -1], # 270
|
67
|
-
[1, -1] # 315
|
68
|
-
]
|
69
|
-
@y_pos += dist * directions[360 / @angle - 2][0]
|
70
|
-
@x_pos += dist * directions[360 / @angle - 2][1]
|
71
|
-
|
72
|
-
self.update @arena, x_old, y_old
|
22
|
+
def move dist=10
|
23
|
+
@actions << [:move, dist]
|
73
24
|
end
|
74
25
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
26
|
+
def update
|
27
|
+
return if @actions.empty?
|
28
|
+
case @actions[0][0]
|
29
|
+
when :move then
|
30
|
+
# I probably should implement dist = @actions[0][1]; dist %= 0.5 or something
|
31
|
+
# but right now I'm tired, and worried I will screw up the math if I try that...
|
32
|
+
dist = @actions[0][1] <= 0.9 ? @actions[0][1] : 1.0
|
33
|
+
@x += Gosu::offset_x @angle, dist
|
34
|
+
@y += Gosu::offset_y @angle, dist
|
35
|
+
@x %= 640
|
36
|
+
@y %= 480
|
83
37
|
|
84
|
-
|
85
|
-
|
38
|
+
@cur_image += 0.1
|
39
|
+
@cur_image %= 7.0
|
86
40
|
|
87
|
-
|
88
|
-
|
89
|
-
arena or return
|
90
|
-
@arena.spawn Projectile.new 'bullet', @id, @x_pos, @y_pos, @angle
|
91
|
-
end
|
41
|
+
@actions[0][1] -= 0.5
|
42
|
+
@actions.shift if @actions[0][1] == 0.0
|
92
43
|
|
93
|
-
|
44
|
+
when :turn then
|
45
|
+
deg = @actions[0][1] <= 9 ? @actions[0][1] : 10
|
94
46
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
# Called whenever another robot is seen within the facing angle.
|
101
|
-
# It usually has some code to fire at the target robot.
|
102
|
-
# Make sure to check that it is not your robot's clone!
|
103
|
-
def when_find_robot robot, target_robot
|
104
|
-
end
|
47
|
+
@angle += deg
|
48
|
+
@angle %= 360
|
49
|
+
@actions[0][1] -= deg
|
105
50
|
|
106
|
-
|
107
|
-
|
51
|
+
@actions.shift if @actions[0][1] == 0
|
52
|
+
end
|
108
53
|
end
|
109
54
|
|
110
|
-
|
111
|
-
|
55
|
+
def draw
|
56
|
+
@images[@cur_image].draw_rot @x, @y, 1, @angle - 90 % 360
|
57
|
+
@image.draw_rot @x, @y, 1, @angle - 90 % 360
|
112
58
|
end
|
113
59
|
end
|
data/test/test_arena.rb
CHANGED
@@ -3,18 +3,23 @@ require 'gembots'
|
|
3
3
|
require 'gembots/arena'
|
4
4
|
require 'gembots/bot'
|
5
5
|
|
6
|
-
class
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
class MyBot1 < Gembots::Robot
|
7
|
+
def on_idle
|
8
|
+
self.move
|
9
|
+
self.turn
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class MyBot2 < Gembots::Robot
|
14
|
+
def on_idle
|
15
|
+
self.move 50
|
16
|
+
self.turn 180
|
11
17
|
end
|
18
|
+
end
|
12
19
|
|
13
|
-
|
14
|
-
|
15
|
-
arena = Gembots::Arena.new
|
16
|
-
|
17
|
-
bot.move 1
|
18
|
-
assert_equal bot.id, arena.board[1][0][0]
|
20
|
+
class ArenaTest < Test::Unit::TestCase
|
21
|
+
def test_arena_window
|
22
|
+
arena = Gembots::Arena.new MyBot1, MyBot2
|
23
|
+
arena.show
|
19
24
|
end
|
20
25
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gembots
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.1'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- L8D
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: gosu
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -48,7 +62,6 @@ files:
|
|
48
62
|
- lib/gembots/arena.rb
|
49
63
|
- lib/gembots/bot.rb
|
50
64
|
- test/test_arena.rb
|
51
|
-
- test/test_bot.rb
|
52
65
|
homepage: http://github.com/L8D/gembots
|
53
66
|
licenses:
|
54
67
|
- MIT
|
@@ -69,10 +82,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
82
|
version: '0'
|
70
83
|
requirements: []
|
71
84
|
rubyforge_project:
|
72
|
-
rubygems_version: 2.0.
|
85
|
+
rubygems_version: 2.0.3
|
73
86
|
signing_key:
|
74
87
|
specification_version: 4
|
75
88
|
summary: Create your own gembots and battle them in a cli arena
|
76
89
|
test_files:
|
77
90
|
- test/test_arena.rb
|
78
|
-
- test/test_bot.rb
|
data/test/test_bot.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'gembots/bot'
|
3
|
-
|
4
|
-
def make_bot
|
5
|
-
bot = Gembots::Robot.new
|
6
|
-
bot.name = "test"
|
7
|
-
bot.turn 90
|
8
|
-
bot.move 5
|
9
|
-
bot
|
10
|
-
end
|
11
|
-
|
12
|
-
class BotTest < Test::Unit::TestCase
|
13
|
-
def test_name
|
14
|
-
bot = make_bot
|
15
|
-
assert_equal "test", bot.name
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_position
|
19
|
-
bot = make_bot
|
20
|
-
assert_equal 5, bot.x_pos
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_clone
|
24
|
-
bot = make_bot
|
25
|
-
bot_clone = bot.clone
|
26
|
-
assert_equal bot_clone.parent_id, bot.id
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_id
|
30
|
-
bot = make_bot
|
31
|
-
assert_equal bot.object_id, bot.id
|
32
|
-
end
|
33
|
-
end
|