Class: SDC::Map

Inherits:
Object
  • Object
show all
Includes:
SDCMeta::AIBackend
Defined in:
lib/core/Map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SDCMeta::AIBackend

#add_ai_page, #ai_running?, #ai_script, #current_ai, #master_ai_running?, #setup_ai, #tick_ai

Constructor Details

#initialize(view_width: 20, view_height: 20) ⇒ Map

Returns a new instance of Map.



13
14
15
16
17
18
19
20
21
# File 'lib/core/Map.rb', line 13

def initialize(view_width: 20, view_height: 20)
	@view_width = view_width
	@view_height = view_height

	@map_layers = []

	@config = nil
	setup_ai
end

Instance Attribute Details

#configObject

TODO: Remove this if unneeded



11
12
13
# File 'lib/core/Map.rb', line 11

def config
  @config
end

#map_layersObject

TODO: Remove this if unneeded



11
12
13
# File 'lib/core/Map.rb', line 11

def map_layers
  @map_layers
end

Instance Method Details

#draw(window, offset) ⇒ Object



92
93
94
95
96
# File 'lib/core/Map.rb', line 92

def draw(window, offset)
	@map_layers.each do |layer|
		window.draw_translated(layer, (layer.z ? layer.z : 0), offset)
	end
end

#load_from_file(filename) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/core/Map.rb', line 30

def load_from_file(filename)
	# TODO: Read properties from file
	@number_of_layers = 3
	@width = 100
	@height = 100
	@tile_width = 60
	@tile_height = 60

	# Can be used for more detailed collisions
	@tile_shape = SDC::CollisionShapeBox.new(SDC::Coordinates.new(-@tile_width * 0.5, -@tile_height * 0.5), SDC::Coordinates.new(@tile_width, @tile_height))

	@number_of_layers.times do |i|
		new_layer = SDC::MapLayer.new(@width, @height, @view_width, @view_height, @tile_width, @tile_height)

		# TODO: Load tiles into map layer and initialize the mesh
		new_layer.load_test_map
		new_layer.collision_active = (i == 2)

		@map_layers.push(new_layer)
	end
end

#master_ai_scriptObject



98
99
100
# File 'lib/core/Map.rb', line 98

def master_ai_script
	@config.run_script if @config
end

#reload(position) ⇒ Object



88
89
90
# File 'lib/core/Map.rb', line 88

def reload(position)
	@map_layers.each {|layer| layer.reload(position)}
end

#set_config(name) ⇒ Object



23
24
25
26
27
28
# File 'lib/core/Map.rb', line 23

def set_config(name)
	@config = SDC::Data::map_configs[name].dup
	@map_layers.each do |layer|
		layer.link_tileset(SDC::Data::tilesets[@config.tileset_index])
	end
end

#test_collision_with_entity(entity) ⇒ Object



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
81
82
# File 'lib/core/Map.rb', line 52

def test_collision_with_entity(entity)
	boxes = entity.boxes
	ex = entity.position.x
	ey = entity.position.y

	any_result = false

	boxes.each do |box|
		ix_low = ((ex + box.offset.x - box.origin.x * box.scale.x) / @tile_width).floor
		iy_low = ((ey + box.offset.y - box.origin.y * box.scale.y) / @tile_height).floor
		ix_high = ((ex + box.size.x * box.scale.x + box.offset.x - box.origin.x * box.scale.x) / @tile_width).floor
		iy_high = ((ey + box.size.y * box.scale.y + box.offset.y - box.origin.y * box.scale.y) / @tile_height).floor

		# TODO: Implement default value for map layers, so this loop can be extended over undefined areas

		[0, ix_low].max.upto([ix_high, @width - 1].min) do |ix|
			[0, iy_low].max.upto([iy_high, @height - 1].min) do |iy|
				@map_layers.each do |layer|
					next if !layer.collision_active
					# TODO: Include detection for empty tiles and tile properties in general
					tile_id = layer[ix, iy]
					result = layer.tileset.tiles[tile_id].solid
					any_result = true
					# NOTE: Use   Collider.test(<shape>, <pos>, @tile_shape, Coordinates.new((ix + 0.5) * @tile_width, (iy + 0.5) * @tile_height))   for detailed collisions
					return true if result
				end
			end
		end
	end
	return !any_result
end

#updateObject



84
85
86
# File 'lib/core/Map.rb', line 84

def update
	tick_ai
end