voxelamming 0.2.0 → 0.3.0

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: fd127d3be01c9626c89f8c30261e386d693f003e4fd5d98ada6e429d768d64df
4
- data.tar.gz: 424aaa454b52aeee6fd8a4d2c986422e31824e1e66949d4d4dedf94806038799
3
+ metadata.gz: e4040f3049c1f2d0d1a15fa25a7e53ed734d96301653ecae91f1f9c6739dbddb
4
+ data.tar.gz: 571a79bcde4c693b16e209d04096096b56248c39c3516e96d880671daf80a839
5
5
  SHA512:
6
- metadata.gz: 8affe5c143e5f915ff5d19bf9763c01474ea091431c3d77e62dc0d2b2728cbe8f19e03dcd74a5b86f6460baac0a1f157a57929debacc9d92e38918b81ab300a9
7
- data.tar.gz: 9705d0420d514da88f9551ccdb74b06f5260fbb18afef47f89fe74589917897e85aa5c84560388a24c3f7b017da0d3681d63e94edabfb9cd5a87743a702e4608
6
+ metadata.gz: e9fb8453f785ddb3d94c422b2bdd0e17ac2789a102695eb7141bdee11de3236ae7e10b8b53fb441d98bebcde351cf9a6159b84a4d69496a32c5b3c4d9e3297c8
7
+ data.tar.gz: 944de153ff5b15c6276a9f5b6ca62e59f138f3fe0166ded32558b72d73d497984fbb38ed1bfd63664d8dd864a5f5683eeeef3b39373a0bf0ef8635943f6e748b
data/CODE_OF_CONDUCT.md CHANGED
@@ -60,7 +60,7 @@ representative at an online or offline event.
60
60
 
61
61
  Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
62
  reported to the community leaders responsible for enforcement at
63
- [INSERT CONTACT METHOD].
63
+ creativival@gmail.com.
64
64
  All complaints will be reviewed and investigated promptly and fairly.
65
65
 
66
66
  All community leaders are obligated to respect the privacy and security of the
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # VoxelammingGem
1
+ # Voxelamming
2
2
 
3
3
  This Ruby package converts Python code into JSON format and sends it to the Voxelamming app using WebSockets, allowing users to create 3D voxel models by writing Ruby scripts.
4
4
 
@@ -26,27 +26,27 @@ gem install voxelamming
26
26
  require 'voxelamming'
27
27
 
28
28
  room_name = '1000'
29
- build_box = Voxelamming::BuildBox.new(room_name)
29
+ vox = Voxelamming::VoxelammingManager.new(room_name)
30
30
 
31
- build_box.set_box_size(0.5)
32
- build_box.set_build_interval(0.01)
31
+ vox.set_box_size(0.5)
32
+ vox.set_build_interval(0.01)
33
33
 
34
34
  for i in 0...100
35
- build_box.create_box(-1, i, 0, r: 0, g: 1, b: 1)
36
- build_box.create_box(0, i, 0, r: 1, g: 0, b: 0)
37
- build_box.create_box(1, i, 0, r: 1, g: 1, b: 0)
38
- build_box.create_box(2, i, 0, r: 0, g: 1, b: 1)
35
+ vox.create_box(-1, i, 0, r: 0, g: 1, b: 1)
36
+ vox.create_box(0, i, 0, r: 1, g: 0, b: 0)
37
+ vox.create_box(1, i, 0, r: 1, g: 1, b: 0)
38
+ vox.create_box(2, i, 0, r: 0, g: 1, b: 1)
39
39
  end
40
40
 
41
41
  for i in 0...50
42
- build_box.remove_box(0, i * 2, 0)
43
- build_box.remove_box(1, i * 2 + 1, 0)
42
+ vox.remove_box(0, i * 2, 0)
43
+ vox.remove_box(1, i * 2 + 1, 0)
44
44
  end
45
45
 
46
- build_box.send_data
46
+ vox.send_data
47
47
  ```
48
48
 
49
- This code snippet demonstrates a simple example where a red voxel is created at a specific location. You can use various functions provided by the `BuildBox` class to build more complex models.
49
+ This code snippet demonstrates a simple example where a red voxel is created at a specific location. You can use various functions provided by the `VoxelammingManager` class to build more complex models.
50
50
 
51
51
  #### Method description
52
52
 
@@ -75,6 +75,12 @@ This code snippet demonstrates a simple example where a red voxel is created at
75
75
  | `frame_out()` | Ends recording a frame. | |
76
76
  | `set_frame_fps(fps)` | Sets the frame rate (default: 2). | `fps`: Frame rate (int) |
77
77
  | `set_frame_repeats(repeats)` | Sets the number of frame repetitions (default: 10). | `repeats`: Number of repetitions (int) |
78
+ | Game Method Name | Description | Arguments |
79
+ | `set_game_screen(width, height, angle=90, r=1, g=1, b=0, alpha=0.5)` | Sets the game screen size. | `width`, `height`: screen size (float), `angle`: angle (float), `r`, `g`, `b`, `alpha`: color (float, 0-1) |
80
+ | `set_game_score(score)` | Sets the game score. | `score`: game score (int) |
81
+ | `send_game_over()` | Triggers game over. | |
82
+ | `create_sprite(sprite_name, color_list, x, y, direction=90, scale=1, visible=True)` | Creates a sprite. | `sprite_name`: sprite name (string), `color_list`: dot color data (string), `x`, `y`: position (float), `direction`: angle (float), `scale`: scale (float), `visible`: visibility (boolean) |
83
+ | `move_sprite(sprite_name, x, y, direction=90, scale=1, visible=True)` | Moves a sprite. | `sprite_name`: sprite name (string), `x`, `y`: position (float), `direction`: angle (float), `scale`: scale (float), `visible`: visibility (boolean) |
78
84
 
79
85
  ## Development
80
86
 
@@ -92,4 +98,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
92
98
 
93
99
  ## Code of Conduct
94
100
 
95
- Everyone interacting in the VoxelammingGem project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/voxelamming_gem/blob/master/CODE_OF_CONDUCT.md).
101
+ Everyone interacting in the Voxelamming project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/voxelamming_gem/blob/master/CODE_OF_CONDUCT.md).
data/index.html CHANGED
@@ -2,7 +2,7 @@
2
2
  <html>
3
3
  <head>
4
4
  <meta charset="UTF-8">
5
- <title>VoxelammingGem</title>
5
+ <title>Voxelamming</title>
6
6
  <style>
7
7
  body {
8
8
  font-family: sans-serif;
@@ -58,7 +58,7 @@
58
58
  </style>
59
59
  </head>
60
60
  <body>
61
- <h1><a href="#voxelamminggem" id="voxelamminggem"></a>VoxelammingGem</h1>
61
+ <h1><a href="#voxelamminggem" id="voxelamminggem"></a>Voxelamming</h1>
62
62
  <p>This Ruby package converts Python code into JSON format and sends it to the Voxelamming app using WebSockets, allowing users to create 3D voxel models by writing Ruby scripts.</p>
63
63
  <h2><a href="#whats-voxelamming" id="whats-voxelamming"></a>What's Voxelamming?</h2>
64
64
  <p align="center"><img src="https://creativival.github.io/voxelamming/image/voxelamming_icon.png" alt="Voxelamming Logo" width="200"/></p>
@@ -75,26 +75,26 @@
75
75
  <pre><code class="ruby">require 'voxelamming'
76
76
 
77
77
  room_name = '1000'
78
- build_box = Voxelamming::BuildBox.new(room_name)
78
+ vox = Voxelamming::VoxelammingManager.new(room_name)
79
79
 
80
- build_box.set_box_size(0.5)
81
- build_box.set_build_interval(0.01)
80
+ vox.set_box_size(0.5)
81
+ vox.set_build_interval(0.01)
82
82
 
83
83
  for i in 0...100
84
- build_box.create_box(-1, i, 0, r: 0, g: 1, b: 1)
85
- build_box.create_box(0, i, 0, r: 1, g: 0, b: 0)
86
- build_box.create_box(1, i, 0, r: 1, g: 1, b: 0)
87
- build_box.create_box(2, i, 0, r: 0, g: 1, b: 1)
84
+ vox.create_box(-1, i, 0, r: 0, g: 1, b: 1)
85
+ vox.create_box(0, i, 0, r: 1, g: 0, b: 0)
86
+ vox.create_box(1, i, 0, r: 1, g: 1, b: 0)
87
+ vox.create_box(2, i, 0, r: 0, g: 1, b: 1)
88
88
  end
89
89
 
90
90
  for i in 0...50
91
- build_box.remove_box(0, i * 2, 0)
92
- build_box.remove_box(1, i * 2 + 1, 0)
91
+ vox.remove_box(0, i * 2, 0)
92
+ vox.remove_box(1, i * 2 + 1, 0)
93
93
  end
94
94
 
95
- build_box.send_data
95
+ vox.send_data
96
96
  </code></pre>
97
- <p>This code snippet demonstrates a simple example where a red voxel is created at a specific location. You can use various functions provided by the <code>BuildBox</code> class to build more complex models.</p>
97
+ <p>This code snippet demonstrates a simple example where a red voxel is created at a specific location. You can use various functions provided by the <code>VoxelammingManager</code> class to build more complex models.</p>
98
98
  <h4><a href="#method-description" id="method-description"></a>Method description</h4>
99
99
  <table>
100
100
  <thead>
@@ -134,6 +134,6 @@ build_box.send_data
134
134
  <h2><a href="#license" id="license"></a>License</h2>
135
135
  <p>The gem is available as open source under the terms of the <a href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
136
136
  <h2><a href="#code-of-conduct" id="code-of-conduct"></a>Code of Conduct</h2>
137
- <p>Everyone interacting in the VoxelammingGem project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the <a href="https://github.com/[USERNAME]/voxelamming_gem/blob/master/CODE_OF_CONDUCT.md">code of conduct</a>.</p>
137
+ <p>Everyone interacting in the Voxelamming project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the <a href="https://github.com/[USERNAME]/voxelamming_gem/blob/master/CODE_OF_CONDUCT.md">code of conduct</a>.</p>
138
138
  </body>
139
139
  </html>
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Voxelamming
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/voxelamming.rb CHANGED
@@ -17,7 +17,7 @@ module Voxelamming
17
17
  end
18
18
 
19
19
  # Main process
20
- class BuildBox
20
+ class VoxelammingManager
21
21
  @@texture_names = ["grass", "stone", "dirt", "planks", "bricks"]
22
22
  @@model_names = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto", "Sun",
23
23
  "Moon", "ToyBiplane", "ToyCar", "Drummer", "Robot", "ToyRocket", "RocketToy1", "RocketToy2", "Skull"]
@@ -33,11 +33,15 @@ module Voxelamming
33
33
  @animation = [0, 0, 0, 0, 0, 0, 1, 0]
34
34
  @boxes = []
35
35
  @frames = []
36
- @sentence = []
36
+ @sentences = []
37
37
  @lights = []
38
38
  @commands = []
39
39
  @models = []
40
40
  @model_moves = []
41
+ @sprites = []
42
+ @sprite_moves = []
43
+ @game_score = -1
44
+ @game_screen = [] # width, height, angle=90, red=1, green=1, blue=1, alpha=0.5
41
45
  @size = 1
42
46
  @shape = 'box'
43
47
  @is_metallic = 0
@@ -46,6 +50,9 @@ module Voxelamming
46
50
  @build_interval = 0.01
47
51
  @is_framing = false
48
52
  @frame_id = 0
53
+ @websocket = nil
54
+ @last_sent_time = nil
55
+ @timer = nil
49
56
  end
50
57
 
51
58
  def clear_data
@@ -58,7 +65,11 @@ module Voxelamming
58
65
  @animation = [0, 0, 0, 0, 0, 0, 1, 0]
59
66
  @boxes = []
60
67
  @frames = []
61
- @sentence = []
68
+ @sentences = []
69
+ @sprites = []
70
+ @sprite_moves = []
71
+ @game_score = -1
72
+ @game_screen = [] # width, height, angle=90, red=1, green=1, blue=1, alpha=0.5
62
73
  @lights = []
63
74
  @commands = []
64
75
  @models = []
@@ -119,8 +130,8 @@ module Voxelamming
119
130
  x, y, z = round_numbers([x, y, z])
120
131
 
121
132
  # Compute the rotation after transform
122
- translate_rotation_matrix = get_rotation_matrix(-pitch, -yaw, -roll)
123
- rotate_matrix = matrix_multiply(translate_rotation_matrix, base_rotation_matrix)
133
+ transform_rotation_matrix = get_rotation_matrix(-pitch, -yaw, -roll)
134
+ rotate_matrix = matrix_multiply(transform_rotation_matrix, base_rotation_matrix)
124
135
  @matrix_transform = [x, y, z, *rotate_matrix[0], *rotate_matrix[1], *rotate_matrix[2]]
125
136
  else
126
137
  x, y, z = round_numbers([x, y, z])
@@ -196,11 +207,11 @@ module Voxelamming
196
207
  @build_interval = interval
197
208
  end
198
209
 
199
- def write_sentence(sentence, x, y, z, r: 1, g: 1, b: 1, alpha: 1)
210
+ def write_sentence(sentence, x, y, z, r: 1, g: 1, b: 1, alpha: 1, font_size: 8, is_fixed_width: false)
200
211
  x, y, z = round_numbers([x, y, z]).map(&:to_s)
201
212
  r, g, b, alpha = round_two_decimals([r, g, b, alpha])
202
- r, g, b, alpha = [r, g, b, alpha].map(&:floor).map(&:to_s)
203
- @sentence = [sentence, x, y, z, r, g, b, alpha]
213
+ r, g, b, alpha, font_size = [r, g, b, alpha, font_size].map(&:floor).map(&:to_s)
214
+ @sentences << [sentence, x, y, z, r, g, b, alpha, font_size, is_fixed_width ? "1" : "0"]
204
215
  end
205
216
 
206
217
  def set_light(x, y, z, r: 1, g: 1, b: 1, alpha: 1, intensity: 1000, interval: 1, light_type: 'point')
@@ -308,6 +319,65 @@ module Voxelamming
308
319
  @model_moves << [entity_name, x, y, z, pitch, yaw, roll, scale]
309
320
  end
310
321
 
322
+ # Game API
323
+
324
+ def set_game_screen(width, height, angle = 90, r = 1, g = 1, b = 0, alpha = 0.5)
325
+ @game_screen = [width, height, angle, r, g, b, alpha]
326
+ end
327
+
328
+ def set_game_score(score)
329
+ @game_score = score.to_f
330
+ end
331
+
332
+ def send_game_over
333
+ @commands << 'gameOver'
334
+ end
335
+
336
+ def set_rotation_style(sprite_name, rotation_style = 'all around')
337
+ @rotation_styles[sprite_name] = rotation_style
338
+ end
339
+
340
+ def create_sprite(sprite_name, color_list, x, y, direction = 0, scale = 1, visible = true)
341
+ # 新しいスプライトデータを配列に追加
342
+ x, y, direction = round_numbers([x, y, direction])
343
+ x, y, direction, scale = [x, y, direction, scale].map(&:to_s)
344
+ @sprites << [sprite_name, color_list, x, y, direction, scale, visible ? '1' : '0']
345
+ end
346
+
347
+ def move_sprite(sprite_name, x, y, direction = 0, scale = 1, visible = true)
348
+ # x, y, directionを丸める
349
+ x, y, direction = round_numbers([x, y, direction])
350
+ x, y, direction, scale = [x, y, direction, scale].map(&:to_s)
351
+
352
+ # rotation_styleを取得
353
+ if @rotation_styles.has_key?(sprite_name)
354
+ rotation_style = @rotation_styles[sprite_name]
355
+
356
+ # rotation_styleが変更された場合、新しいスプライトデータを配列に追加
357
+ case rotation_style
358
+ when 'left-right'
359
+ direction_mod = direction.to_i % 360 # 常に0から359の範囲で処理(常に正の数になる)
360
+ if direction_mod > 90 && direction_mod < 270
361
+ direction = '-180' # -180は左右反転するようにボクセラミング側で実装されている
362
+ else
363
+ direction = '0'
364
+ end
365
+ when "don't rotate"
366
+ direction = '0'
367
+ else
368
+ direction = direction.to_s
369
+ end
370
+ else
371
+ # rotation_styleが設定されていない場合、そのままの値を使う
372
+ direction = direction.to_s
373
+ end
374
+
375
+ # sprites配列から同じスプライト名の要素を削除
376
+ @sprite_moves.reject! { |sprite_info| sprite_info[0] == sprite_name }
377
+
378
+ # 新しいスプライトデータを配列に追加
379
+ @sprite_moves << [sprite_name, x, y, direction, scale, visible ? '1' : '0']
380
+ end
311
381
 
312
382
  def send_data(name: '')
313
383
  puts 'send_data'
@@ -319,11 +389,15 @@ module Voxelamming
319
389
  "animation": @animation,
320
390
  "boxes": @boxes,
321
391
  "frames": @frames,
322
- "sentence": @sentence,
392
+ "sentences": @sentences,
323
393
  "lights": @lights,
324
394
  "commands": @commands,
325
395
  "models": @models,
326
396
  "modelMoves": @model_moves,
397
+ "sprites": @sprites,
398
+ "spriteMoves": @sprite_moves,
399
+ "gameScore": @game_score,
400
+ "gameScreen": @game_screen,
327
401
  "size": @size,
328
402
  "shape": @shape,
329
403
  "interval": @build_interval,
@@ -335,35 +409,61 @@ module Voxelamming
335
409
  }.to_json
336
410
 
337
411
  EM.run do
338
- ws = Faye::WebSocket::Client.new('wss://websocket.voxelamming.com')
339
-
340
- ws.on :open do |_event|
341
- p [:open]
342
- puts 'WebSocket connection open'
343
- ws.send(@room_name)
344
- puts "Joined room: #{@room_name}"
345
- ws.send(data_to_send)
346
- puts data_to_send
347
- puts 'Sent data to server'
348
-
349
- EM.add_timer(1) do
350
- ws.close
412
+ if @websocket.nil?
413
+ @websocket = Faye::WebSocket::Client.new('wss://websocket.voxelamming.com')
414
+
415
+ @websocket.on :open do |_event|
416
+ p [:open]
417
+ puts 'WebSocket connection open'
418
+ @websocket.send(@room_name)
419
+ puts "Joined room: #{@room_name}"
420
+ send_data_to_server(data_to_send)
421
+ end
422
+
423
+ @websocket.on :error do |event|
424
+ puts "WebSocket error: #{event.message}"
351
425
  EM.stop
352
426
  end
353
- end
354
427
 
355
- ws.on :error do |event|
356
- puts "WebSocket error: #{event.message}"
357
- EM.stop
428
+ @websocket.on :close do |_event|
429
+ puts 'WebSocket connection closed'
430
+ EM.stop
431
+ end
432
+ else
433
+ send_data_to_server(data_to_send)
358
434
  end
435
+ end
436
+ end
437
+
438
+ def send_data_to_server(data)
439
+ if @websocket && @websocket.ready_state == Faye::WebSocket::API::OPEN
440
+ @websocket.send(data)
441
+ puts 'Sent data to server'
442
+ @last_sent_time = Time.now
443
+
444
+ reset_close_timer
445
+ end
446
+ end
359
447
 
360
- ws.on :close do |_event|
361
- puts 'WebSocket connection closed'
362
- EM.stop
448
+ def reset_close_timer
449
+ EM.cancel_timer(@timer) if @timer
450
+
451
+ @timer = EM.add_timer(2) do
452
+ if Time.now - @last_sent_time >= 2
453
+ close_connection
363
454
  end
364
455
  end
365
456
  end
366
457
 
458
+ def close_connection
459
+ if @websocket
460
+ puts 'Closing WebSocket connection.'
461
+ @websocket.close
462
+ @websocket = nil
463
+ EM.stop
464
+ end
465
+ end
466
+
367
467
  def round_numbers(num_list)
368
468
  if @is_allowed_float == 1
369
469
  round_two_decimals(num_list)
@@ -381,8 +481,8 @@ module Voxelamming
381
481
  class Turtle
382
482
  include Math
383
483
 
384
- def initialize(build_box)
385
- @build_box = build_box
484
+ def initialize(voxelamming_instance)
485
+ @vox = voxelamming_instance
386
486
  @x = 0
387
487
  @y = 0
388
488
  @z = 0
@@ -400,7 +500,7 @@ module Voxelamming
400
500
  x, y, z = x.round(3), y.round(3), z.round(3)
401
501
 
402
502
  if @drawable
403
- @build_box.draw_line(@x, @y, @z, x, y, z, r: @color[0], g: @color[1], b: @color[2])
503
+ @vox.draw_line(@x, @y, @z, x, y, z, r: @color[0], g: @color[1], b: @color[2])
404
504
  end
405
505
 
406
506
  @x = x
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: voxelamming
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - creativival
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-10 00:00:00.000000000 Z
11
+ date: 2024-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faye-websocket
@@ -42,6 +42,7 @@ files:
42
42
  - lib/voxelamming.rb
43
43
  - lib/voxelamming/version.rb
44
44
  - sig/voxelamming.rbs
45
+ - voxelamming-0.2.0.gem
45
46
  homepage: https://creativival.github.io/voxelamming
46
47
  licenses:
47
48
  - MIT