Class: SpriteKit::Map

Inherits:
Object
  • Object
show all
Defined in:
mygame/lib/sprite_kit/map.rb

Overview

The data side of the map editor. Holds layers of tiles, an undo / redo history, and handles saving / loading. Rendering and input live in SpriteKit::MapEditor.

Tiles are plain sprite hashes in world space:

{ x:, y:, w:, h:, path:, source_x:, source_y:, source_w:, source_h: }

Examples:

map = SpriteKit::Map.load("data/map.json")
map.layers.each do |layer|
  args.outputs.sprites.concat(layer.tiles) if layer.visible
end

Constant Summary collapse

VERSION =
1
MAX_HISTORY =
200

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: "data/map.json", tile_w: 16, tile_h: 16, layers: nil) ⇒ Map

Returns a new instance of Map.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'mygame/lib/sprite_kit/map.rb', line 23

def initialize(path: "data/map.json", tile_w: 16, tile_h: 16, layers: nil)
  @path = path
  @tile_w = tile_w
  @tile_h = tile_h
  @layers = layers || []
  @undo_stack = []
  @redo_stack = []

  if @layers.length < 1
    @layers << create_layer
  end

  @commands = {
    tiles: {
      execute: -> (change) {
        layer = layer_by_id(change.layer_id)
        change.removed.each { |tile| layer.tiles.delete(tile) }
        layer.tiles.concat(change.added)
      },
      undo: -> (change) {
        layer = layer_by_id(change.layer_id)
        change.added.each { |tile| layer.tiles.delete(tile) }
        layer.tiles.concat(change.removed)
      }
    },
    add_layer: {
      execute: -> (change) {
        @layers.insert(change[:index], change[:layer])
        @current_layer_index = change[:index]
      },
      undo: -> (change) {
        @layers.delete_at(change[:index])
        clamp_current_layer
      }
    },
    remove_layer: {
      execute: -> (change) {
        @layers.delete_at(change[:index])
        clamp_current_layer
      },
      undo: -> (change) {
        @layers.insert(change[:index], change[:layer])
        @current_layer_index = change[:index]
      }
    },
    move_layer: {
      execute: -> (change) {
        swap_layers(change[:from], change[:to])
        @current_layer_index = change[:to]
      },
      undo: -> (change) {
        swap_layers(change[:to], change[:from])
        @current_layer_index = change[:from]
      }
    }
  }
  @current_layer_index = @layers.length - 1
end

Instance Attribute Details

#current_layer_index ⇒ Object

Returns the value of attribute current_layer_index.



20
21
22
# File 'mygame/lib/sprite_kit/map.rb', line 20

def current_layer_index
  @current_layer_index
end

#layers ⇒ Object

Returns the value of attribute layers.



20
21
22
# File 'mygame/lib/sprite_kit/map.rb', line 20

def layers
  @layers
end

#path ⇒ Object

Returns the value of attribute path.



20
21
22
# File 'mygame/lib/sprite_kit/map.rb', line 20

def path
  @path
end

#redo_stack ⇒ Object (readonly)

Returns the value of attribute redo_stack.



21
22
23
# File 'mygame/lib/sprite_kit/map.rb', line 21

def redo_stack
  @redo_stack
end

#tile_h ⇒ Object

Returns the value of attribute tile_h.



20
21
22
# File 'mygame/lib/sprite_kit/map.rb', line 20

def tile_h
  @tile_h
end

#tile_w ⇒ Object

Returns the value of attribute tile_w.



20
21
22
# File 'mygame/lib/sprite_kit/map.rb', line 20

def tile_w
  @tile_w
end

#undo_stack ⇒ Object (readonly)

Returns the value of attribute undo_stack.



21
22
23
# File 'mygame/lib/sprite_kit/map.rb', line 21

def undo_stack
  @undo_stack
end

Class Method Details

.load(path, **kwargs) ⇒ Object

Returns a new map from the file at path, or an empty map if the file doesn't exist.



83
84
85
86
87
# File 'mygame/lib/sprite_kit/map.rb', line 83

def self.load(path, **kwargs)
  map = new(path: path, **kwargs)
  map.load
  map
end

Instance Method Details

#add_layer(name: nil) ⇒ Object

Inserts a new layer above the current layer and makes it current.



107
108
109
110
111
112
113
114
# File 'mygame/lib/sprite_kit/map.rb', line 107

def add_layer(name: nil)
  layer = create_layer(name: name)
  index = @current_layer_index + 1
  @layers.insert(index, layer)
  @current_layer_index = index
  push_history({ command: :add_layer, layer: layer, index: index })
  layer
end

#begin_change(layer = current_layer) ⇒ Object

Starts a new set of tile changes against a layer. Pass it to place_tile / erase, then commit it. All changes in a single commit get undone / redone together (ie: a brush stroke).



145
146
147
# File 'mygame/lib/sprite_kit/map.rb', line 145

def begin_change(layer = current_layer)
  { command: :tiles, layer_id: layer.id, added: [], removed: [] }
end

#can_redo? ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'mygame/lib/sprite_kit/map.rb', line 200

def can_redo?
  @redo_stack.length > 0
end

#can_undo? ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'mygame/lib/sprite_kit/map.rb', line 196

def can_undo?
  @undo_stack.length > 0
end

#commit(change) ⇒ Object



168
169
170
171
172
173
# File 'mygame/lib/sprite_kit/map.rb', line 168

def commit(change)
  return false if change.added.length <= 0 && change.removed.length <= 0

  push_history(change)
  true
end

#create_layer(name: nil) ⇒ Object



97
98
99
100
101
102
103
104
# File 'mygame/lib/sprite_kit/map.rb', line 97

def create_layer(name: nil)
  {
    id: GTK.create_uuid,
    name: name || "Layer #{@layers.length + 1}",
    visible: true,
    tiles: []
  }
end

#current_layer ⇒ Object



89
90
91
# File 'mygame/lib/sprite_kit/map.rb', line 89

def current_layer
  @layers[@current_layer_index]
end

#erase(rect, change) ⇒ Object

Removes every tile on the change's layer that intersects rect.



159
160
161
162
163
164
165
166
# File 'mygame/lib/sprite_kit/map.rb', line 159

def erase(rect, change)
  layer = layer_by_id(change.layer_id)
  intersecting_tiles = Geometry.find_all_intersect_rect(rect, layer.tiles)
  return intersecting_tiles if intersecting_tiles.length <= 0

  intersecting_tiles.each { |tile| remove_tile(layer, tile, change) }
  intersecting_tiles
end

#layer_by_id(id) ⇒ Object



93
94
95
# File 'mygame/lib/sprite_kit/map.rb', line 93

def layer_by_id(id)
  @layers.find { |layer| layer.id == id }
end

#load(path = @path) ⇒ Boolean

Returns true if a map file was loaded.

Returns:

  • (Boolean) —

    true if a map file was loaded.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'mygame/lib/sprite_kit/map.rb', line 247

def load(path = @path)
  return false if !GTK.stat_file(path)

  json = GTK.parse_json_file(path)
  return false if !json

  data = HashMethods.symbolize_keys(json)

  @tile_w = data.tile_w || @tile_w
  @tile_h = data.tile_h || @tile_h

  layers = (data.layers || []).map.with_index do |layer, index|
    {
      id: layer.id || GTK.create_uuid,
      name: layer.name || "Layer #{index + 1}",
      visible: layer.visible != false,
      tiles: layer.tiles || []
    }
  end

  @layers = layers if layers.length > 0
  @current_layer_index = @layers.length - 1
  @undo_stack.clear
  @redo_stack.clear
  true
rescue => e
  puts "SpriteKit::Map: Unable to load #{path}: #{e}"
  false
end

#move_layer(index, direction) ⇒ Object

Moves a layer up (+1) or down (-1) the draw order.



129
130
131
132
133
134
135
136
# File 'mygame/lib/sprite_kit/map.rb', line 129

def move_layer(index, direction)
  to = index + direction
  return if to < 0 || to > @layers.length - 1

  swap_layers(index, to)
  @current_layer_index = to
  push_history({ command: :move_layer, from: index, to: to })
end

#place_tile(tile, change) ⇒ Object

Places a tile, replacing anything it overlaps on the same layer.



150
151
152
153
154
155
156
# File 'mygame/lib/sprite_kit/map.rb', line 150

def place_tile(tile, change)
  layer = layer_by_id(change.layer_id)
  erase(tile, change)
  layer.tiles << tile
  change.added << tile
  tile
end

#redo ⇒ Object



213
214
215
216
217
218
219
220
# File 'mygame/lib/sprite_kit/map.rb', line 213

def redo
  change = @redo_stack.pop
  return false if !change

  execute_change(change)
  @undo_stack << change
  true
end

#remove_layer(index = @current_layer_index) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'mygame/lib/sprite_kit/map.rb', line 116

def remove_layer(index = @current_layer_index)
  # Can't remove the last layer, the editor always needs something to draw on.
  return if @layers.length <= 1

  layer = @layers.delete_at(index)
  return if !layer

  clamp_current_layer
  push_history({ command: :remove_layer, layer: layer, index: index })
  layer
end

#save(path = @path) ⇒ Object



242
243
244
# File 'mygame/lib/sprite_kit/map.rb', line 242

def save(path = @path)
  GTK.write_file(path, to_json)
end

#serialize ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'mygame/lib/sprite_kit/map.rb', line 222

def serialize
  {
    version: VERSION,
    tile_w: @tile_w,
    tile_h: @tile_h,
    layers: @layers.map do |layer|
      {
        id: layer.id,
        name: layer.name,
        visible: layer.visible,
        tiles: layer.tiles
      }
    end
  }
end

#tile_at(point) ⇒ Hash?

Finds the top-most visible tile containing the point.

Returns:

  • (Hash, nil) —

    { tile:, layer_index: }



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'mygame/lib/sprite_kit/map.rb', line 177

def tile_at(point)
  (@layers.length - 1).downto(0) do |index|
    layer = @layers[index]
    next if !layer.visible

    tile = layer.tiles.reverse_each.find do |t|
      point.x >= t.x && point.x < t.x + t.w && point.y >= t.y && point.y < t.y + t.h
    end

    return { tile: tile, layer_index: index } if tile
  end

  nil
end

#tile_count ⇒ Object



192
193
194
# File 'mygame/lib/sprite_kit/map.rb', line 192

def tile_count
  @layers.sum { |layer| layer.tiles.length }
end

#to_json ⇒ Object



238
239
240
# File 'mygame/lib/sprite_kit/map.rb', line 238

def to_json
  SpriteKit::Serializer.to_json(serialize)
end

#toggle_layer_visibility(index = @current_layer_index) ⇒ Object



138
139
140
141
# File 'mygame/lib/sprite_kit/map.rb', line 138

def toggle_layer_visibility(index = @current_layer_index)
  layer = @layers[index]
  layer.visible = !layer.visible if layer
end

#undo ⇒ Object



204
205
206
207
208
209
210
211
# File 'mygame/lib/sprite_kit/map.rb', line 204

def undo
  change = @undo_stack.pop
  return false if !change

  revert_change(change)
  @redo_stack << change
  true
end