tungsten-engine 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 27921d9333ce3bedeb6fc32b8a04a1834397eace
4
+ data.tar.gz: 16e385f0f877b34a409fa0142d40fc6fa2bec91c
5
+ SHA512:
6
+ metadata.gz: b673865162cd6f03fa8889703b2f8d2e8c1cc70f5a4021da2568a3e2c1eaf45fe0cd054401bbfd35d8916ca5d0a2df53e55c1aa4d691c5d23910c9c0ec28ecef
7
+ data.tar.gz: 54d3ca04c56fcdd3b4fb07b45a432954daa5de5e5eadcc9f49fae00909cff8ca4c8cfdec5c2d59d882d98d613fd9f588d080e3318ec7b24ae1b5a50e6c8dd1b0
data/lib/core/game.rb ADDED
@@ -0,0 +1,67 @@
1
+ require_relative('./time_span')
2
+ require_relative('./game_time')
3
+ require_relative('./game_component')
4
+
5
+ module TungstenEngine
6
+ module Core
7
+ # Represents the base class for a real game
8
+ class Game
9
+ attr_accessor :fixed_time_step
10
+
11
+ def initialize(fixed_time_step = true, target_frame_rate = 60)
12
+ @components = []
13
+ @fixed_time_step = fixed_time_step
14
+ self.target_frame_rate = target_frame_rate
15
+ end
16
+
17
+ def target_frame_rate=(frame_rate)
18
+ @target_frame_microseconds = 1_000_000.0 / frame_rate
19
+ end
20
+
21
+ def register_component(component)
22
+ raise("Cannot register objects that ain't GameComponent instances") \
23
+ unless component.is_a? GameComponent
24
+ end
25
+
26
+ def run
27
+ started = Time.now
28
+ last_loop = Time.now
29
+
30
+ game_loop = Thread.new do
31
+ now = Time.now
32
+ total = TimeSpan.diff(started, now)
33
+ elapsed = TimeSpan.diff(last_loop, now)
34
+ spare = TimeSpan.diff(elapsed.microseconds, @target_frame_microseconds)
35
+ slow = spare < 0
36
+ if @fixed_time_step && !slow
37
+ Thread.current.sleep(spare.seconds)
38
+ now = Time.now
39
+ total = TimeSpan.diff(started, now)
40
+ elapsed = TimeSpan.diff(last_loop, now)
41
+ # do not re-calculate 'slow' here, since we want to know if we
42
+ # have been to slow when the loop was entered again, not if we maybe
43
+ # are too slow after waiting
44
+ end
45
+ game_time = GameTime.new(total, elapsed, slow)
46
+ update(game_time)
47
+ render(game_time)
48
+ last_loop = now
49
+ end
50
+ game_loop.join
51
+ end
52
+
53
+ def update(game_time)
54
+ @components.each do |component|
55
+ component.update(game_time)
56
+ end
57
+ end
58
+
59
+ def render(game_time)
60
+ @components.each do |component|
61
+ component.render(game_time)
62
+ end
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,16 @@
1
+ module TungstenEngine
2
+ module Core
3
+ # Represents a modular component of the game,
4
+ # being updated and rendered automatically after
5
+ # being registered to the game
6
+ class GameComponent
7
+ def update(_game_time)
8
+ raise('This method needs to be overwritten in a subclass')
9
+ end
10
+
11
+ def render(_game_time)
12
+ raise('This method needs to be overwritten in a subclass')
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module TungstenEngine
2
+ module Core
3
+ # Structure holding the times passed
4
+ # since the game started and
5
+ # since the last game loop ran
6
+ class GameTime
7
+ attr_reader :total, :elapsed, :running_slow
8
+
9
+ def initialize(total, elapsed, running_slow)
10
+ @total = total
11
+ @elapsed = elapsed
12
+ @running_slow = running_slow
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,66 @@
1
+ module TungstenEngine
2
+ module Core
3
+ # Represents the span between two points in time
4
+ class TimeSpan
5
+ attr_accessor :milliseconds
6
+
7
+ def self.from_microseconds(microseconds)
8
+ TimeSpan.from_milliseconds(microseconds.to_d / 1000.0)
9
+ end
10
+
11
+ def self.from_milliseconds(milliseconds)
12
+ me = TimeSpan.new
13
+ me.milliseconds = milliseconds
14
+ return me
15
+ end
16
+
17
+ def self.from_seconds(seconds)
18
+ TimeSpan.from_milliseconds(seconds.to_d * 1000)
19
+ end
20
+
21
+ def self.from_minutes(minutes)
22
+ TimeSpan.from_milliseconds(minutes.to_d * 60000)
23
+ end
24
+
25
+ def self.from_hours(hours)
26
+ TimeSpan.from_milliseconds(hours.to_d * 3600000)
27
+ end
28
+
29
+ def self.from_days(days)
30
+ TimeSpan.from_milliseconds(days.to_d * 86400000)
31
+ end
32
+
33
+ def self.from_years(years)
34
+ TimeSpan.from_days(years.to_d * 365.242)
35
+ end
36
+
37
+ def self.diff(start_date_time, end_date_time)
38
+ TimeSpan.from_seconds(end_date_time - start_date_time)
39
+ end
40
+
41
+ def microseconds
42
+ milliseconds.to_d * 1000
43
+ end
44
+
45
+ def seconds
46
+ milliseconds.to_d * 0.001
47
+ end
48
+
49
+ def minutes
50
+ seconds.to_d * 0.0166667
51
+ end
52
+
53
+ def hours
54
+ minutes.to_d * 0.0166667
55
+ end
56
+
57
+ def days
58
+ hours.to_d * 0.0416667
59
+ end
60
+
61
+ def years
62
+ days.to_d * 0.00273791
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ require_relative('./core/time_span')
2
+ require_relative('./core/game_time')
3
+ require_relative('./core/game_component')
4
+ require_relative('./core/game')
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tungsten-engine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Meschke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple game engine for e.g. text based adventures played on the console
14
+ email: thomas.meschke@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/core/game.rb
20
+ - lib/core/game_component.rb
21
+ - lib/core/game_time.rb
22
+ - lib/core/time_span.rb
23
+ - lib/tungsten-engine.rb
24
+ homepage: https://rubygems.org/gems/tungsten-engine
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.5.2.1
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Tungsten Engine
48
+ test_files: []