Commit 05565c3229ab50eb417a6fa8ca1abeeecbeba219

Authored by Marius Hanne
1 parent 7c7e4bccdd
Exists in master

framerate independent simulation

Showing 7 changed files with 25 additions and 22 deletions Side-by-side Diff

... ... @@ -36,7 +36,7 @@
36 36  
37 37 include Rubygame
38 38  
39   - attr_reader :screen, :queue, :clock, :objects, :player, :enemies, :level, :opts
  39 + attr_reader :screen, :queue, :clock, :objects, :player, :enemies, :level, :opts, :steps
40 40 attr_accessor :state, :space, :options
41 41 def initialize options
42 42 @options = options
43 43  
44 44  
... ... @@ -51,13 +51,16 @@
51 51 @enemies = []
52 52 @queue = EventQueue.new
53 53 @clock = Clock.new
54   - @clock.target_framerate = 600
  54 + @clock.target_framerate = 60
55 55 @keys_pressed = []
56   - @background = Surface.load(File.join(data_dir, "images", "background.png")).zoom_to(@screen.size[0], @screen.size[1] * 2).to_display
  56 + @background = Surface.load(File.join(data_dir, "images", "background.png"))
  57 + .zoom_to(@screen.size[0], @screen.size[1] * 2)
  58 + .to_display
57 59 @bg_pos = @background.size[1] / 2
58 60 @level = options[:level]
59 61 @state = :ready
60 62 @opts = opts
  63 + @steps = 0
61 64 Music.autoload_dirs = [ File.join(data_dir, "sound") ]
62 65 end
63 66  
... ... @@ -92,6 +95,9 @@
92 95  
93 96 flash_text(text: "Level #{@level}", alpha: 150)
94 97 loop do
  98 + passed = @clock.tick
  99 + @steps += passed
  100 + @space.step(passed)
95 101  
96 102 @queue.enable_new_style_events
97 103 @queue.each do |event|
... ... @@ -124,9 +130,6 @@
124 130 flash_text(text: "Level #{@level}")
125 131 @bg_pos = @background.size[1] / 2
126 132 end
127   -
128   - @space.step(1)
129   - Clock.wait(0.1 - @clock.tick / 1000) # TODO
130 133 end
131 134 end
132 135  
... ... @@ -205,7 +208,7 @@
205 208  
206 209 def font name, size = 10
207 210 @fonts ||= {}
208   - @fonts[name.to_sym] = TTF.new(File.join(data_dir, "fonts", "#{name}.ttf"), size)
  211 + @fonts[name.to_sym] ||= TTF.new(File.join(data_dir, "fonts", "#{name}.ttf"), size)
209 212 end
210 213 end
211 214  
... ... @@ -9,7 +9,7 @@
9 9 m: 1000.0,
10 10 p: [0.0, 0.0],
11 11 v: [0.0, 0.0],
12   - v_limit: 100.0,
  12 + v_limit: 10.0,
13 13 rot: 0.0,
14 14 ctype: :object,
15 15 }
... ... @@ -5,7 +5,7 @@
5 5  
6 6 @pos = [rand(game.screen.size[0] - size[0]), 0 - size[1]]
7 7 @body.p = CP::Vec2.new(*@pos)
8   - @body.v += CP::Vec2.new(0.0, @params[:speed])
  8 + @body.v += CP::Vec2.new(0.0, @params[:speed] / 10.0)
9 9 @thread = Thread.start do
10 10 loop do
11 11 break unless @visible
... ... @@ -3,7 +3,7 @@
3 3 FILE = "/sys/bus/i2c/drivers/bma180/2-0041/coord"
4 4  
5 5 def self.read
6   - d = File.read(FILE).strip.split(",")[0..1].map(&:to_f).map{|v| v / 50}
  6 + d = File.read(FILE).strip.split(",")[0..1].map(&:to_f).map{|v| v / 50_000}
7 7 [-d[0], d[1]]
8 8 end
9 9  
... ... @@ -19,10 +19,10 @@
19 19 when :right
20 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
1 1 class Ship < Movable
2 2  
3 3 SHIPS = {
4   - glider: { power: 50, shield: 20, speed: 3, weapons: [:laser],
  4 + glider: { power: 50, shield: 20, speed: 2.5, weapons: [:laser],
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 }},
1 1 class Weapon
2 2  
3 3 WEAPONS = {
4   - laser: { damage: 10, speed: 25, freq: 980,
  4 + laser: { damage: 10, speed: 15, freq: 500,
5 5 image: { image: "bullet.png", zoom: 1.0 }},
6   - missile: { damage: 20, speed: 10, freq: 950,
  6 + missile: { damage: 20, speed: 5, freq: 300,
7 7 image: { image: "bullet.png", zoom: 2.0 }},
8   - torpedo: { damage: 30, speed: 5, freq: 300,
9   - image: { image: "bullet.png",zoom: 5.0 }},
  8 + torpedo: { damage: 30, speed: 1, freq: 100,
  9 + image: { image: "bullet.png", zoom: 3.0 }},
10 10 }
11 11  
12 12 attr_reader :ship, :game, :type, :params
13 13  
14 14  
... ... @@ -17,13 +17,13 @@
17 17 end
18 18  
19 19 def fire
20   - if game.clock.ticks - @last_shot >= (1000 - params[:freq])
  20 + if game.steps - @last_shot >= (1000 - params[:freq])
21 21 calc = @ship.is_a?(Player) ? :sub : :add
22   - v = @ship.body.v.to_a.send(calc, [0.0, params[:speed]])
  22 + v = @ship.body.v.to_a.send(calc, [0.0, params[:speed] / 10.0])
23 23 opts = params[:image].merge(p: @ship.pos,
24 24 v: v, shooter: @ship, damage: params[:damage], m: params[:damage])
25 25 Bullet.new(game, opts).show
26   - @last_shot = game.clock.ticks
  26 + @last_shot = game.steps
27 27 end
28 28 end
29 29