good_luck_wizard 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: 0b6d9433bd142292329a04da71e53365da906d1e73d96c7424a91adc2fa3f7b6
4
- data.tar.gz: 4e9c31089c9976fdc8f756b257f349c0a5d238f5e7bef1c6a813a918caa6da8d
3
+ metadata.gz: dbc38af238a6cc80858a6e316e300aedff0c0fe0936a3bbd76856236c94881f1
4
+ data.tar.gz: 7e709dcc1afb14905d4c33e07dd1781b228e2c9ec7e25433ff73e9f944b1a03c
5
5
  SHA512:
6
- metadata.gz: ca4eba9034e385f7112ae9837f4029e684874ef11bc0d054d5ca8eb7816c94ba928d02077f634edb5ddb355188d76afc9ddae7aaf2a650097344e7bac55f4fd2
7
- data.tar.gz: d4e3fdd503ddf4d775419b98e756572813c63b02085574605ca7e284754ac79f6a6375129b29acf5441f4d1129536eecc4458546c963228f5b7d34bdc78076c4
6
+ metadata.gz: 847f1f7fa71a3bb0a213c25e69d8f30fd43419018977641d53a25f6bc87e88baaeb40ce90c0600eb3f097fb5d9bb39bfda4682e55d1f669f807050aa7720d8c4
7
+ data.tar.gz: f84e32458ad72740728373a24318ae41fa87e3568a51f11a55e11c8be6570e6ecea8d1039408210177122bcba3f865c8bcefd6bed0030f86107e085943008a0a
data/lib/glw.rb CHANGED
@@ -9,30 +9,25 @@ module GLW
9
9
  initialize_zeitwerk
10
10
 
11
11
  GLW::Term.with_term do |t|
12
- t.set(x: 0, y: 0, c: "@")
13
- t.refresh
14
- sleep 1
12
+ g = Game.new(term: t)
15
13
 
16
- t.set(x: 0, y: 0, c: " ")
17
- t.set(x: 1, y: 1, c: "@")
18
- t.refresh
19
- sleep 1
20
-
21
- t.set(x: 1, y: 1, c: " ")
22
- t.set(x: 2, y: 2, c: "@")
23
- t.refresh
24
- sleep 1
14
+ loop do
15
+ g.render
16
+ break if g.send_key(t.getch) == :quit
17
+ end
25
18
  end
26
19
  end
27
20
 
28
- private
29
-
30
21
  def initialize_zeitwerk
22
+ return if @zeitwerk_enabled
23
+
31
24
  Zeitwerk::Loader.new.tap do |loader|
32
25
  loader.inflector.inflect("glw" => "GLW")
33
26
  loader.push_dir(__dir__)
34
27
  loader.setup
35
28
  end
29
+
30
+ @zeitwerk_enabled = true
36
31
  end
37
32
  end
38
33
  end
@@ -0,0 +1,31 @@
1
+ module GLW
2
+ class Game
3
+ def initialize(term:)
4
+ @map = Map.new
5
+ @term = term
6
+ end
7
+
8
+ def send_key(key)
9
+ case key
10
+ when "q"
11
+ :quit
12
+ when "h"
13
+ @map.send_event :left
14
+ when "l"
15
+ @map.send_event :right
16
+ when "k"
17
+ @map.send_event :up
18
+ when "j"
19
+ @map.send_event :down
20
+ end
21
+ end
22
+
23
+ def render
24
+ map.render(term, width: term.width - 60, height: term.height)
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :term, :map
30
+ end
31
+ end
@@ -0,0 +1,71 @@
1
+ module GLW
2
+ class Map
3
+ MAP = <<~TXT
4
+ ############
5
+ #..........# ##########
6
+ #..........############....#...#
7
+ #..............................#
8
+ #.......g..############....#...#
9
+ #..........# #....#...#
10
+ ############ ##########
11
+ TXT
12
+
13
+ def initialize
14
+ @player_x = 3
15
+ @player_y = 3
16
+
17
+ @map = Hash.new(" ")
18
+ MAP.lines.each_with_index do |line, y|
19
+ line.chars.each_with_index do |c, x|
20
+ @map[[x, y]] = c
21
+ end
22
+ end
23
+
24
+ @offset_x = 1
25
+ @offset_y = 1
26
+ end
27
+
28
+ def send_event(e)
29
+ case e
30
+ when :left
31
+ @player_x -= 1 if valid_player_position?(@player_x - 1, @player_y)
32
+ when :right
33
+ @player_x += 1 if valid_player_position?(@player_x + 1, @player_y)
34
+ when :up
35
+ @player_y -= 1 if valid_player_position?(@player_x, @player_y - 1)
36
+ when :down
37
+ @player_y += 1 if valid_player_position?(@player_x, @player_y + 1)
38
+ end
39
+ end
40
+
41
+ def render(term, width:, height:)
42
+ (0..width - 1).each do |x|
43
+ (0..height - 1).each do |y|
44
+
45
+ if [@player_x + @offset_x, @player_y + @offset_y] == [x, y]
46
+ term.set(
47
+ x: x,
48
+ y: y,
49
+ c: "@",
50
+ fg: 220
51
+ )
52
+ else
53
+ term.set(
54
+ x: x,
55
+ y: y,
56
+ c: @map[[x - @offset_x, y - @offset_y]]
57
+ )
58
+ end
59
+ end
60
+ end
61
+
62
+ term.refresh
63
+ end
64
+
65
+ private
66
+
67
+ def valid_player_position?(x, y)
68
+ @map[[x, y]] == "."
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,33 @@
1
+ module GLW
2
+ class Random
3
+ MASK64 = (1 << 64) - 1
4
+ MASK32 = (1 << 32) - 1
5
+ CONST = 0x2545F4914F6CDD1D
6
+
7
+ def initialize(seed)
8
+ @state = seed & MASK64
9
+ end
10
+
11
+ def sample(collection)
12
+ collection[integer % collection.length]
13
+ end
14
+
15
+ def integer
16
+ x = state
17
+ x = (x ^ (x >> 12)) & MASK64
18
+ x = (x ^ (x << 25)) & MASK64
19
+ x = (x ^ (x >> 27)) & MASK64
20
+ self.state = x
21
+
22
+ (((x * CONST) & MASK64) >> 32) & MASK32
23
+ end
24
+
25
+ def float
26
+ integer.to_f / (1 << 32)
27
+ end
28
+
29
+ private
30
+
31
+ attr_accessor :state
32
+ end
33
+ end
@@ -6,23 +6,9 @@ module GLW
6
6
  DEFAULT_FG = 255
7
7
  DEFAULT_BG = 16
8
8
 
9
- Cell = Struct.new(:c, :fg, :bg) do
10
- def initialize(*args)
11
- super
12
- @dirty = true
13
- end
14
-
15
- def seen?
16
- !@dirty
17
- end
18
-
19
- def seen!
20
- @dirty = false
21
- end
22
- end
9
+ Cell = Struct.new(:c, :fg, :bg)
23
10
 
24
11
  DEFAULT_CELL = Cell.new(" ", DEFAULT_FG, DEFAULT_BG)
25
- DEFAULT_CELL.seen!
26
12
 
27
13
  class << self
28
14
  def with_term
@@ -43,6 +29,7 @@ module GLW
43
29
  @window = window
44
30
  @tiles = Hash.new { DEFAULT_CELL }
45
31
  @next_tiles = {}
32
+ @colors = TermColors.new
46
33
  end
47
34
 
48
35
  def set(x:, y:, c:, fg: DEFAULT_FG, bg: DEFAULT_BG)
@@ -51,10 +38,26 @@ module GLW
51
38
  @next_tiles[[x, y]] = cell unless @tiles[[x, y]] == cell
52
39
  end
53
40
 
41
+ def width
42
+ Curses.cols
43
+ end
44
+
45
+ def height
46
+ Curses.lines
47
+ end
48
+
49
+ def getch
50
+ window.getch
51
+ end
52
+
54
53
  def refresh
55
54
  @next_tiles.each do |coords, cell|
56
- window.setpos *coords.reverse
57
- window << cell.c
55
+ x, y = *coords
56
+ window.setpos(y, x)
57
+
58
+ window.attron(colors[cell.fg, cell.bg]) do
59
+ window << cell.c
60
+ end
58
61
  end
59
62
 
60
63
  @tiles = @tiles.merge(@next_tiles)
@@ -64,6 +67,6 @@ module GLW
64
67
 
65
68
  private
66
69
 
67
- attr_reader :window
70
+ attr_reader :window, :colors
68
71
  end
69
72
  end
@@ -0,0 +1,22 @@
1
+ module GLW
2
+ class TermColors
3
+ def initialize(curses = Curses)
4
+ index = 1
5
+
6
+ @colors = Hash.new do |hash, key|
7
+ fg, bg = *key
8
+
9
+ curses.init_pair(index, fg, bg)
10
+ hash[key] = curses.color_pair(index)
11
+
12
+ index += 1
13
+
14
+ hash[key]
15
+ end
16
+ end
17
+
18
+ def [](fg, bg)
19
+ @colors[[fg, bg]]
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module GLW
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: good_luck_wizard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jared Norman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-31 00:00:00.000000000 Z
11
+ date: 2021-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses
@@ -127,7 +127,11 @@ files:
127
127
  - exe/glw
128
128
  - good_luck_wizard.gemspec
129
129
  - lib/glw.rb
130
+ - lib/glw/game.rb
131
+ - lib/glw/map.rb
132
+ - lib/glw/random.rb
130
133
  - lib/glw/term.rb
134
+ - lib/glw/term_colors.rb
131
135
  - lib/glw/version.rb
132
136
  homepage: http://github.com/jarednorman/glw
133
137
  licenses: []