Skip to content

Commit

Permalink
tilemap collision demo
Browse files Browse the repository at this point in the history
  • Loading branch information
cbscribe committed Dec 20, 2018
1 parent 347d475 commit f3d52aa
Show file tree
Hide file tree
Showing 22 changed files with 940 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.import
logs/
10 changes: 10 additions & 0 deletions tilemap_collision/Ball.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends RigidBody2D

signal collided

func _integrate_forces(state):
for i in state.get_contact_count():
var collider = state.get_contact_collider_object(i)
var cpos = state.get_contact_collider_position(i)
var n = state.get_contact_local_normal(i)
emit_signal('collided', collider, cpos, n)
44 changes: 44 additions & 0 deletions tilemap_collision/Ball.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://Ball.gd" type="Script" id=1]
[ext_resource path="res://icon.png" type="Texture" id=2]

[sub_resource type="CircleShape2D" id=1]

custom_solver_bias = 0.0
radius = 32.1762

[node name="Ball" type="RigidBody2D"]

input_pickable = false
collision_layer = 1
collision_mask = 1
mode = 0
mass = 1.0
friction = 1.0
bounce = 1.0
gravity_scale = 5.0
custom_integrator = false
continuous_cd = 0
contacts_reported = 1
contact_monitor = true
sleeping = false
can_sleep = true
linear_velocity = Vector2( -200, 0 )
linear_damp = 0.0
angular_velocity = 0.0
angular_damp = 0.0
script = ExtResource( 1 )
_sections_unfolded = [ "Angular", "Linear" ]

[node name="Sprite" type="Sprite" parent="." index="0"]

modulate = Color( 1, 0, 0, 1 )
texture = ExtResource( 2 )
_sections_unfolded = [ "Visibility" ]

[node name="CollisionShape2D" type="CollisionShape2D" parent="." index="1"]

shape = SubResource( 1 )


34 changes: 34 additions & 0 deletions tilemap_collision/Character/Character.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Simple K2D character for testing/demos
extends KinematicBody2D

signal collided

export var gravity = 2500
export var speed = 400
export var jump = -900

var jumping = false
var velocity = Vector2()

func get_input():
velocity.x = 0
jumping = false
if Input.is_action_pressed('ui_right'):
velocity.x += 1
if Input.is_action_pressed('ui_left'):
velocity.x -= 1
velocity.x *= speed
if Input.is_action_just_pressed('ui_select'):
jumping = true

func _physics_process(delta):
velocity.y += gravity * delta
get_input()
velocity = move_and_slide(velocity, Vector2(0, -1))
for i in get_slide_count():
var collision = get_slide_collision(i)
if collision:
emit_signal('collided', collision)

if is_on_floor() and jumping:
velocity.y = jump
57 changes: 57 additions & 0 deletions tilemap_collision/Character/Character.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://Character/Character.gd" type="Script" id=1]
[ext_resource path="res://icon.png" type="Texture" id=2]

[sub_resource type="RectangleShape2D" id=1]

custom_solver_bias = 0.0
extents = Vector2( 24.8646, 32.2074 )

[node name="Character" type="KinematicBody2D"]

input_pickable = false
collision_layer = 1
collision_mask = 1
collision/safe_margin = 0.08
script = ExtResource( 1 )
gravity = 2500
speed = 400
jump = -900

[node name="Sprite" type="Sprite" parent="." index="0"]

texture = ExtResource( 2 )
region_rect = Rect2( 242, 150, 39, 48 )
_sections_unfolded = [ "Region" ]

[node name="CollisionShape2D" type="CollisionShape2D" parent="." index="1"]

shape = SubResource( 1 )

[node name="Camera2D" type="Camera2D" parent="." index="2"]

anchor_mode = 1
rotating = false
current = true
zoom = Vector2( 1, 1 )
limit_left = -10000000
limit_top = -10000000
limit_right = 10000000
limit_bottom = 10000000
limit_smoothed = false
drag_margin_h_enabled = true
drag_margin_v_enabled = true
smoothing_enabled = false
smoothing_speed = 5.0
offset_v = 0.0
offset_h = 0.0
drag_margin_left = 0.2
drag_margin_top = 0.2
drag_margin_right = 0.2
drag_margin_bottom = 0.2
editor_draw_screen = true
editor_draw_limits = false
editor_draw_drag_margin = false


26 changes: 26 additions & 0 deletions tilemap_collision/Main.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extends Node2D

var collision_pos = []

func _process(delta):
var cpos = $TileMap.world_to_map($Character.position)
$CanvasLayer/GridContainer/TilePos.text = str(cpos)
var mpos = $TileMap.world_to_map(get_global_mouse_position())
$CanvasLayer/GridContainer/MousePos.text = str(mpos)

func _on_Character_collided(collision):
# KinematicCollision2D object emitted by character
if collision.collider is TileMap:
var tile_pos = collision.collider.world_to_map($Character.position)
tile_pos -= collision.normal # Colliding tile
var tile = collision.collider.get_cellv(tile_pos)
if tile > 0:
$TileMap.set_cellv(tile_pos, tile-1)

func _on_Ball_collided(collider, pos, normal):
if collider is TileMap:
var tile_pos = collider.world_to_map(pos - normal)
var tile = collider.get_cellv(tile_pos)
if tile > 0:
collider.set_cellv(tile_pos, tile-1)

165 changes: 165 additions & 0 deletions tilemap_collision/Main.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
[gd_scene load_steps=6 format=2]

[ext_resource path="res://Main.gd" type="Script" id=1]
[ext_resource path="res://fonts/RobotoBold24.tres" type="DynamicFont" id=2]
[ext_resource path="res://color_tiles_red.tres" type="TileSet" id=3]
[ext_resource path="res://Character/Character.tscn" type="PackedScene" id=4]
[ext_resource path="res://Ball.tscn" type="PackedScene" id=5]

[node name="Main" type="Node2D" index="0"]

script = ExtResource( 1 )
_sections_unfolded = [ "Z Index" ]

[node name="CanvasLayer" type="CanvasLayer" parent="." index="0"]

editor/display_folded = true
layer = 1
offset = Vector2( 0, 0 )
rotation = 0.0
scale = Vector2( 1, 1 )
transform = Transform2D( 1, 0, 0, 1, 0, 0 )

[node name="GridContainer" type="GridContainer" parent="CanvasLayer" index="0"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_left = 18.0
margin_top = 17.0
margin_right = 1024.0
margin_bottom = 105.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 1
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 1
columns = 2
_sections_unfolded = [ "custom_constants" ]

[node name="Label2" type="Label" parent="CanvasLayer/GridContainer" index="0"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_right = 79.0
margin_bottom = 29.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 4
custom_fonts/font = ExtResource( 2 )
text = "Player:"
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1
_sections_unfolded = [ "custom_fonts" ]

[node name="TilePos" type="Label" parent="CanvasLayer/GridContainer" index="1"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_left = 83.0
margin_right = 122.0
margin_bottom = 29.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 4
custom_fonts/font = ExtResource( 2 )
text = "0, 1"
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1
_sections_unfolded = [ "custom_fonts" ]

[node name="Label3" type="Label" parent="CanvasLayer/GridContainer" index="2"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_top = 33.0
margin_right = 79.0
margin_bottom = 62.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 4
custom_fonts/font = ExtResource( 2 )
text = "Mouse:"
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1
_sections_unfolded = [ "custom_fonts" ]

[node name="MousePos" type="Label" parent="CanvasLayer/GridContainer" index="3"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_left = 83.0
margin_top = 33.0
margin_right = 122.0
margin_bottom = 62.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 4
custom_fonts/font = ExtResource( 2 )
text = "0, 1"
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1
_sections_unfolded = [ "custom_fonts" ]

[node name="TileMap" type="TileMap" parent="." index="1"]

mode = 0
tile_set = ExtResource( 3 )
cell_size = Vector2( 64, 64 )
cell_quadrant_size = 16
cell_custom_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
cell_half_offset = 2
cell_tile_origin = 0
cell_y_sort = false
cell_clip_uv = false
collision_use_kinematic = false
collision_friction = 1.0
collision_bounce = 0.0
collision_layer = 1
collision_mask = 1
occluder_light_mask = 1
format = 1
tile_data = PoolIntArray( -65518, 15, 0, 18, 15, 0, 65538, 15, 0, 65554, 15, 0, 131074, 15, 0, 131090, 15, 0, 196610, 15, 0, 196619, 15, 0, 196626, 15, 0, 262146, 15, 0, 262155, 15, 0, 262162, 15, 0, 327682, 15, 0, 327689, 15, 0, 327690, 15, 0, 327691, 15, 0, 327692, 15, 0, 327693, 15, 0, 327694, 15, 0, 327695, 15, 0, 327696, 15, 0, 327697, 15, 0, 327698, 15, 0, 393218, 15, 0, 393219, 15, 0, 393220, 15, 0, 393221, 15, 0, 393222, 15, 0, 393223, 15, 0, 393224, 15, 0, 393225, 15, 0 )
_sections_unfolded = [ "Material", "Visibility" ]

[node name="Character" parent="." index="2" instance=ExtResource( 4 )]

show_behind_parent = true
position = Vector2( 618.057, 269.024 )
_sections_unfolded = [ "Visibility" ]

[node name="Ball" parent="." index="3" instance=ExtResource( 5 )]

position = Vector2( 336.138, 73.674 )

[connection signal="collided" from="Character" to="." method="_on_Character_collided"]

[connection signal="collided" from="Ball" to="." method="_on_Ball_collided"]


26 changes: 26 additions & 0 deletions tilemap_collision/assets/TileSetMaker.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extends Node2D

var tilesize = Vector2(64, 64)
var w = 4
var h = 4

onready var texture = $Sprite.texture

func _ready():
var tex_width = texture.get_width() / tilesize.x
var tex_height = texture.get_width() / tilesize.y
var ts = TileSet.new()
for x in range(tex_width):
for y in range(tex_height):
var region = Rect2(x*tilesize.x, y*tilesize.y, tilesize.x, tilesize.y)
var id = x + y * h
ts.create_tile(id)
ts.tile_set_texture(id, texture)
ts.tile_set_region(id, region)
var s = RectangleShape2D.new()
s.extents = tilesize/2
ts.tile_set_shape(id, 0, s)
var xform = ts.tile_get_shape_transform(id, 0)
xform.origin += tilesize/2
ts.tile_set_shape_transform(id, 0, xform)
ResourceSaver.save("res://color_tiles_red.tres", ts)
15 changes: 15 additions & 0 deletions tilemap_collision/assets/TileSetMaker.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://assets/TileSetMaker.gd" type="Script" id=1]
[ext_resource path="res://assets/number_tiles_red.png" type="Texture" id=2]


[node name="TileSetMaker" type="Node2D"]

script = ExtResource( 1 )

[node name="Sprite" type="Sprite" parent="." index="0"]

texture = ExtResource( 2 )


Binary file added tilemap_collision/assets/number_tiles_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f3d52aa

Please sign in to comment.