Commit a070906aa7653897dc3131721d3e0c539200008a

Authored by Marius Hanne
1 parent 0a44025665
Exists in master

weapons

Showing 7 changed files with 73 additions and 47 deletions Side-by-side Diff

... ... @@ -9,6 +9,7 @@
9 9 require 'explosion'
10 10 require 'movable'
11 11 require 'bullet'
  12 +require 'weapon'
12 13 require 'ship'
13 14 require 'enemy'
14 15 require 'player'
... ... @@ -25,7 +26,11 @@
25 26 zip(other).map {|v| v.inject(op) }
26 27 end
27 28 end
28   -
  29 +class Numeric
  30 + def radians_to_vec2
  31 + CP::Vec2.new(Math::cos(self), Math::sin(self))
  32 + end
  33 +end
29 34 class Game
30 35  
31 36 include Rubygame
... ... @@ -39,6 +44,7 @@
39 44 @screen = Screen.new options[:size], 0, flags
40 45 @screen.title = "test-game"
41 46 @space = CP::Space.new
  47 + @space.damping = 1.0
42 48 setup_collisions
43 49 @objects = []
44 50 @enemies = []
... ... @@ -67,6 +73,7 @@
67 73 bullet = self.objects.find {|o| o.shape == bullet_shape }
68 74 ship = self.objects.find {|o| o.shape == ship_shape }
69 75 next unless bullet && ship
  76 + next if bullet.options[:shooter] == ship
70 77 bullet.hit(ship)
71 78 @space.remove_body(bullet_shape.body)
72 79 @space.remove_shape(bullet_shape)
... ... @@ -99,17 +106,8 @@
99 106 end
100 107  
101 108 # TODO
102   - if @keys_pressed.include?(:space) && @state == :running
103   - @bullet_lock = 0 if @bullet_lock > 10
104   - if @bullet_lock == 0
105   - Bullet.new(self, p: @player.pos.sub([0.0, @player.shape.r]),
106   - dir: :up, type: :torpedo, shooter: @player).show
107   - end
108   - @bullet_lock += 1
109   - else
110   - @bullet_lock = 0
111   - end
112   -
  109 + @player.fire if @keys_pressed.include?(:space) && @state == :running
  110 +
113 111 Enemy.generate(self) if @enemies.size < @level
114 112  
115 113 [:up, :down, :left, :right].each {|d|
1 1 class Bullet < Movable
2 2  
3   - WEAPONS = {
4   - laser: { damage: 1, speed: 50, freq: 100,
5   - image: { image: "bullet.png", zoom: 0.5 }},
6   - torpedo: { damage: 20, speed: 5, freq: 10,
7   - image: { image: "bullet.png",zoom: 2.0 }},
8   - missile: { damage: 10, speed: 25, freq: 50,
9   - image: { image: "bullet.png", zoom: 1.0 }},
10   - }
11   -
12   - attr_reader :damage, :image
13   -
14 3 def initialize game, opts = {}
15   - @params = WEAPONS[opts[:type]]
16   - opts = @params[:image].merge(opts)
17 4 opts[:rot] = opts[:dir] == :down ? 180.0 : 0.0
18 5 opts.merge!(m: 10.0, ctype: :bullet)
19 6 super(game, opts)
20 7 p = opts[:dir] == :down ? pos.add(@shape.r) : pos.sub(@shape.r)
21 8 @body.p = CP::Vec2.new(*p)
22   - v = options[:shooter].body.v.to_a[1].abs + @params[:speed]
23   - @body.v += CP::Vec2.new(0.0, (options[:dir] == :down ? v : 0-v))
24 9 end
25 10  
26 11 def draw screen
... ... @@ -32,8 +17,8 @@
32 17 def hit ship
33 18 p = pos; @options[:dir] == :up ? p[1] -= @shape.r : p[1] += @shape.r
34 19 Explosion.new(@game, p: p).show
35   - ship.take_damage(damage)
36   - @options[:shooter].hit(ship, damage)
  20 + ship.take_damage(options[:damage])
  21 + @options[:shooter].hit(ship, options[:damage])
37 22 hide
38 23 end
39 24 end
... ... @@ -9,12 +9,12 @@
9 9 m: 1000.0,
10 10 p: [0.0, 0.0],
11 11 v: [0.0, 0.0],
12   - v_limit: 1.0,
  12 + v_limit: 100.0,
13 13 rot: 0.0,
14 14 ctype: :object,
15 15 }
16 16  
17   - attr_accessor :size, :shape, :body, :options
  17 + attr_accessor :game, :size, :shape, :body, :options
18 18 def initialize game, opts = {}
19 19 @game, @options = game, DEFAULTS.merge(opts)
20 20 @surface = Surface.load(File.join(@game.data_dir, "images", options[:image]))
... ... @@ -9,9 +9,13 @@
9 9 @thread = Thread.start do
10 10 loop do
11 11 break unless @visible
12   - Bullet.new(@game, p: pos.add([0.0, @shape.r * 2]), dir: :down,
13   - type: :torpedo, shooter: self).show
14   - sleep rand(3) + 1
  12 + begin
  13 + fire
  14 + rescue
  15 + p $!
  16 + puts *$@
  17 + end
  18 + sleep rand(2)
15 19 end
16 20 end
17 21 end
... ... @@ -11,18 +11,18 @@
11 11 def move direction
12 12 case direction
13 13 when :up
14   - @pos[1] -= 10
  14 + @body.v += CP::Vec2.new(0.0, -1.0)
15 15 when :down
16   - @pos[1] += 10
  16 + @body.v += CP::Vec2.new(0.0, 1.0)
17 17 when :left
18   - @pos[0] -= 10
  18 + @body.v += CP::Vec2.new(-1.0, 0.0)
19 19 when :right
20   - @pos[0] += 10
  20 + @body.v += CP::Vec2.new(1.0, 0.0)
21 21 end
22   - @pos[0] = 0 if @pos[0] < 0
23   - @pos[1] = 0 if @pos[1] < 0
24   - @pos[0] = screen.size[0] - size[0] if @pos[0] > screen.size[0] - size[0]
25   - @pos[1] = screen.size[1] - size[1] if @pos[1] > screen.size[1] - size[1]
  22 +# @pos[0] = 0 if @pos[0] < 0
  23 +# @pos[1] = 0 if @pos[1] < 0
  24 +# @pos[0] = screen.size[0] - size[0] if @pos[0] > screen.size[0] - size[0]
  25 +# @pos[1] = screen.size[1] - size[1] if @pos[1] > screen.size[1] - size[1]
26 26 end
27 27  
28 28 def destroy
... ... @@ -5,23 +5,32 @@
5 5 image: { image: "ship.png", zoom: 0.5 }},
6 6 drone: { power: 75, shield: 50, speed: 2, weapons: [:missile],
7 7 image: { image: "ship.png", zoom: 0.7 }},
8   - fighter: { power: 100, shield: 100, speed: 1, weapons: [:laser, :torpedo, :missile],
  8 + fighter: { power: 100, shield: 100, speed: 1, weapons: [
  9 + :laser,
  10 + :torpedo,
  11 + :missile
  12 + ],
9 13 image: { image: "ship.png", zoom: 1.0 }},
10 14 }
11 15  
12 16 attr_accessor :power, :image, :type, :debug
13 17 def initialize game, options
14 18 @params = SHIPS[options[:type]]
15   - options.merge!(m: 1000.0, ctype: :ship)
  19 + options.merge!(m: 1000.0, ctype: :ship, v_limit: 3.0)
16 20 image = @params[:image]
17 21 image[:rot] = is_a?(Enemy) ? 0.0 : 180.0
18 22 super game, options.merge(image)
19 23 @max_power = @params[:power]
20 24 @power = @max_power
  25 + @weapons = @params[:weapons].map {|w| Weapon.new(self, w) }
21 26 end
22 27  
  28 + def fire
  29 + @weapons.each(&:fire)
  30 + end
  31 +
23 32 def take_damage damage
24   - @power -= 20
  33 + @power -= damage
25 34 if @power <= 0
26 35 @power = 1
27 36 destroy
28 37  
... ... @@ -34,9 +43,8 @@
34 43 end
35 44  
36 45 def hit other, damage
37   - @power += 20 / 2
  46 + @power += damage / 5
38 47 @power = @max_power if @power > @max_power
39   - @debug = nil
40 48 end
41 49  
42 50 def destroy
  1 +class Weapon
  2 +
  3 + WEAPONS = {
  4 + laser: { damage: 10, speed: 25, freq: 980,
  5 + image: { image: "bullet.png", zoom: 1.0 }},
  6 + missile: { damage: 20, speed: 10, freq: 950,
  7 + image: { image: "bullet.png", zoom: 2.0 }},
  8 + torpedo: { damage: 30, speed: 5, freq: 300,
  9 + image: { image: "bullet.png",zoom: 5.0 }},
  10 + }
  11 +
  12 + attr_reader :ship, :game, :type, :params
  13 + def initialize ship, type
  14 + @ship, @game, @type, @params = ship, ship.game, type, WEAPONS[type]
  15 + @lock = 0
  16 + @last_shot = 0
  17 + end
  18 +
  19 + def fire
  20 + if game.clock.ticks - @last_shot >= (1000 - params[:freq])
  21 + calc = @ship.is_a?(Player) ? :sub : :add
  22 + v = @ship.body.v.to_a.send(calc, [0.0, params[:speed]])
  23 + opts = params[:image].merge(p: @ship.pos,
  24 + v: v, shooter: @ship, damage: params[:damage], m: params[:damage])
  25 + Bullet.new(game, opts).show
  26 + @last_shot = game.clock.ticks
  27 + end
  28 + end
  29 +
  30 +
  31 +end