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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73f2ffdf6ffebd87c85a2327669a78eae9295ed4
4
- data.tar.gz: a6c7d582df047435d304959ccbe1c727e09251a9
3
+ metadata.gz: 9e7ee5cc5f03ca3b06cda8560005f4be2a60bbe8
4
+ data.tar.gz: 1272a7b540898904aafe8c4635157fbdb7c3b4a7
5
5
  SHA512:
6
- metadata.gz: e2127a038e87c65d91488b74e602be071d58ece17e4e80bb1e8f26854939c93f0e415c6e2856adf7988b3434880afb94a7e62fa92d44fe4074fa47c5f97c365b
7
- data.tar.gz: 19143de086c3a7fafd7f7578c463f78aa7271450579cb98cf3470164f99f46eb62c71e06b2afe0af837cc247a6bfef6e9538e749145aaaabd053e08b000f8968
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
- # The Arena class is used to create and simulate arenas.
4
- class Gembots::Arena
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
- @objects = Hash.new
17
- @board = Array.new(20) { Array.new(20) { [] } }
18
-
19
- # define each bots' update function and add to players hash
20
- bots.each do |bot|
21
- bot.arena = self
22
- @objects[bot.id] = bot
23
- @board[bot.x_pos][bot.y_pos] << bot.id
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
- # Used for updating the board based on changes in robot.
32
- def update_bot robot, x_old, y_old
33
- if x_old != nil and y_old != nil
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
- # set new pos
38
- @board[robot.x_pos][robot.y_pos] << robot.id
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
- # Spawn object into board and objects array.
43
- # Most used for spawning projectiles.
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
- # String contains the robot's name.
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
- # X position relative to the arena.
10
- attr_reader :x_pos
11
-
12
- # Y position relative to the arena.
13
- attr_reader :y_pos
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
- # Returns a duplicate robot(clone) with it's own +parent_id+.
41
- def clone
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
- # Returns true if robot is a clone of target_robot.
48
- def is_clone_of? target_robot
49
- @parent_id == target_robot.id
18
+ def turn angle=10
19
+ @actions << [:turn, angle]
50
20
  end
51
21
 
52
- # Moves the robot forward along it's angle for the distance specified.
53
- # To move backward just use a negative value.
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
- # Rotates angle in degrees clockwise.
76
- # To rotate counter-clockwise just use a negative number.
77
- def turn angle
78
- @angle += angle
79
-
80
- # Used for wrapping:
81
- @angle -= 360 if @angle > 360
82
- @angle += 360 if @angle < 0
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
- self.update @arena
85
- end
38
+ @cur_image += 0.1
39
+ @cur_image %= 7.0
86
40
 
87
- # Spawns projectile of type `bullet` in arena(if there is one).
88
- def fire
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
- # These functions are just for documentation and to prevent erros when calling these undefined.
44
+ when :turn then
45
+ deg = @actions[0][1] <= 9 ? @actions[0][1] : 10
94
46
 
95
- # Called whenever the game is in idle state.
96
- # It is likely used as the robot's main loop.
97
- def when_idle robot
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
- # Called whenever this robot has ended up colliding with another robot.
107
- def when_robot_collision robot, target_robot
51
+ @actions.shift if @actions[0][1] == 0
52
+ end
108
53
  end
109
54
 
110
- # This function is used by the arena for updates and delays and stuff.
111
- def update *i
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 ArenaTest < Test::Unit::TestCase
7
- def test_has_bot
8
- bot = Gembots::Robot.new
9
- arena = Gembots::Arena.new bot
10
- assert_equal bot, arena.objects[bot.id]
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
- def test_board_movement
14
- bot = Gembots::Robot.new
15
- arena = Gembots::Arena.new bot
16
- bot.turn 90
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.0.03
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-06-21 00:00:00.000000000 Z
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.2
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