From e584dbd1158abed6c9e7509412ed5394e02f5aab Mon Sep 17 00:00:00 2001 From: Christoph Heinen Date: Sun, 9 Apr 2017 16:07:39 +0200 Subject: [PATCH] =?UTF-8?q?container=20collision=20l=C3=A4uft?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- container/container.tscn | 23 ++++++++++++++++++++--- level1/level1.tscn | 1 - player/player.tscn | 5 +++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/container/container.tscn b/container/container.tscn index 4153668..ae5711b 100644 --- a/container/container.tscn +++ b/container/container.tscn @@ -1,14 +1,23 @@ -[gd_scene load_steps=3 format=1] +[gd_scene load_steps=4 format=1] [ext_resource path="res://container/Crate.png" type="Texture" id=1] +[sub_resource type="RectangleShape2D" id=2] + +custom_solver_bias = 0.0 +extents = Vector2( 10, 10 ) + [sub_resource type="GDScript" id=1] script/source = "var containertextures = load(\"res://container/all.png\")\nvar numberOfDifContainer = 3\n\nfunc _ready():\n\tset_texture()\n\tprint(\"\")\n\t\nfunc set_texture():\n\trandomize()\n\tvar nr = randi()% numberOfDifContainer\n\tvar texture = AtlasTexture.new()\n\ttexture.set_atlas(containertextures)\n\t#texture.set_region(Rect2(0,0,60,60))\n\ttexture.set_region(getTextureRect(nr))\n\tget_node(\"sprite\").set_texture(texture)\n\n\n\nfunc getTextureRect(nummer):\n\tvar x= nummer % 3 * 60\n\tvar y = 0\n\treturn Rect2(x,y,60,60)" -[node name="container" type="Area2D"] +[node name="container" type="Area2D" groups=[ "container" ]] +transform/pos = Vector2( 41.3129, 31.7331 ) input/pickable = true +shapes/0/shape = SubResource( 2 ) +shapes/0/transform = Matrix32( 2.9397, 0, 0, 2.99475, -18.4918, -3.40633 ) +shapes/0/trigger = true gravity_vec = Vector2( 0, 1 ) gravity = 98.0 linear_damp = 0.1 @@ -17,7 +26,15 @@ script/script = SubResource( 1 ) [node name="sprite" type="Sprite" parent="."] -transform/pos = Vector2( 133.429, 115.807 ) +transform/pos = Vector2( -18.6503, -3.94047 ) texture = ExtResource( 1 ) +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] + +transform/pos = Vector2( -18.4918, -3.40633 ) +transform/scale = Vector2( 2.9397, 2.99475 ) +shape = SubResource( 2 ) +trigger = true +_update_shape_index = 0 + diff --git a/level1/level1.tscn b/level1/level1.tscn index b1804d0..afe005a 100644 --- a/level1/level1.tscn +++ b/level1/level1.tscn @@ -38,6 +38,5 @@ transform/pos = Vector2( 791.126, 671.258 ) [node name="container" parent="." instance=ExtResource( 4 )] transform/pos = Vector2( -128.317, 11.1581 ) -priority = 3.0 diff --git a/player/player.tscn b/player/player.tscn index ef32cb4..74c2fdc 100644 --- a/player/player.tscn +++ b/player/player.tscn @@ -12,7 +12,7 @@ extents = Vector2( 10, 10 ) [sub_resource type="GDScript" id=2] -script/source = "extends RigidBody2D\n\nvar WALK_ACCEL = 800.0\nvar WALK_DEACCEL = 800.0\nvar START_WALK_MAX_VELOCITY = 200.0\nvar WALK_MAX_VELOCITY = 200.0\n\nvar receiveDamage = 0\nvar health = 1000\n\nvar player_size\nvar bullet = preload(\"res://bullet/bullet.tscn\")\n\n#is the player currently shooting\?\nvar shooting = false\nvar shoot_time = 1e20\n\nvar direction = Vector2(1.0, 0.0) #right\n\nfunc _ready():\n\t# Called every time the node is added to the scene.\n\t# Initialization here\n\tplayer_size = get_node(\"player\").get_texture().get_size()\n\tget_node(\"cl0/inventory\").hide()\n\t#get_node(\"cl0/inventory_button\").hide()\n\t#get_node(\"player\").show()\n\t\n\tset_fixed_process(true)\n\nfunc _fixed_process(delta):\n\thealth -= receiveDamage *delta\n\tif(receiveDamage > 0):\n\t\tprint(health)\n\nfunc _integrate_forces(s):\n\tvar speedup = 0\n\tvar damage = 0\n\tvar weaponrange = 0\n\tspeedup = get_node(\"cl0/inventory\").getStats()[0]\n\t#print (speedup)\n\tWALK_MAX_VELOCITY = START_WALK_MAX_VELOCITY + speedup * 20\n\t\n\t#print (WALK_MAX_VELOCITY)\n\t\n\tvar lv = s.get_linear_velocity()\n\tvar step = s.get_step()\n\t\n\t#Inputs\n\tvar move_left = Input.is_action_pressed(\"move_left\")\n\tvar move_right = Input.is_action_pressed(\"move_right\")\n\tvar move_up = Input.is_action_pressed(\"move_up\")\n\tvar move_down = Input.is_action_pressed(\"move_down\")\n\tvar shoot = Input.is_action_pressed(\"shoot\")\n\n\tvar player_rect = Rect2( get_node(\"player\").get_pos() - player_size*0.5, player_size )\n\tvar player_pos = get_node(\"player\").get_pos()\n\t\n\t#Shooting\n\tif(shoot and not shooting):\n\t\tshoot_time = 0\n\t\tvar bi = bullet.instance()\n\t\tvar pos = get_pos() + get_node(\"bullet_shoot\").get_pos()\n\t\t\n\t\tbi.set_pos(pos)\n\t\tget_parent().add_child(bi)\n\t\t\n\t\tbi.set_linear_velocity(direction * 320)\n\t\t#get_node(\"sprite/smoke\").set_emitting(true)\n\t\t#get_node(\"sound\").play(\"shoot\")\n\t\tPS2D.body_add_collision_exception(bi.get_rid(), get_rid()) # Make bullet and this not collide\n\telse:\n\t\tshoot_time += step\n\n\t# MOVING \n\tif (move_left and not move_right): #left\n\t\tif (lv.x > -WALK_MAX_VELOCITY):\n\t\t\tlv.x -= WALK_ACCEL*step\n\t\tdirection = Vector2(-1.0, 0.0)\n\telif (move_right and not move_left): #right\n\t\tif (lv.x < WALK_MAX_VELOCITY):\n\t\t\tlv.x += WALK_ACCEL*step\n\t\tdirection = Vector2(1.0, 0.0)\n\telif (move_down and not move_up): #down\n\t\tif (lv.y < WALK_MAX_VELOCITY):\n\t\t\tlv.y += WALK_ACCEL*step\n\t\tdirection = Vector2(0.0, 1.0)\n\telif (move_up and not move_down): #up\n\t\tif (lv.y > -WALK_MAX_VELOCITY):\n\t\t\tlv.y -= WALK_ACCEL*step\n\t\tdirection = Vector2(0.0, -1.0)\n\telse: #Getting the player to stand still again\n\t\t\tvar xv = abs(lv.x)\n\t\t\tvar yv = abs(lv.y)\n\t\t\t\n\t\t\txv -= WALK_DEACCEL*step\n\t\t\tif (xv < 0):\n\t\t\t\txv = 0\n\t\t\tlv.x = sign(lv.x)*xv\n\t\t\t\n\t\t\tyv -= WALK_DEACCEL*step\n\t\t\tif (yv < 0):\n\t\t\t\tyv = 0\n\t\t\tlv.y = sign(lv.y)*yv\n\t\t\t\n\tshooting = shoot\n\ts.set_linear_velocity(lv)\n\nfunc _on_inventory_pressed():\n\t#get_node(\"cl0/inventory_button\").hide()\n\tget_node(\"cl0/inventory\").show()\n\t#hide()\n\t#print(\"inventory\")\n\tpass # replace with function body\n\n\nfunc _on_player_body_enter( body ):\n\tprint(\"player collision\")\n\tpass # replace with function body\n\n\nfunc _on_Area2D_body_enter( body ):\n\tif(body.is_in_group(\"enemy\")):\n\t\tenemyCollision(body,1)\n\tpass # replace with function body\n\nfunc _on_Area2D_body_exit( body ):\n\tif(body.is_in_group(\"enemy\")):\n\t\tenemyCollision(body,-1)\n\tpass # replace with function body\n\n#add = [1,-1]\nfunc enemyCollision(enemy,add):\n\treceiveDamage += enemy.getDamage() *add\n" +script/source = "extends RigidBody2D\n\nvar WALK_ACCEL = 800.0\nvar WALK_DEACCEL = 800.0\nvar START_WALK_MAX_VELOCITY = 200.0\nvar WALK_MAX_VELOCITY = 200.0\n\nvar receiveDamage = 0\nvar health = 1000\n\nvar player_size\nvar bullet = preload(\"res://bullet/bullet.tscn\")\n\n#is the player currently shooting\?\nvar shooting = false\nvar shoot_time = 1e20\n\nvar direction = Vector2(1.0, 0.0) #right\n\nfunc _ready():\n\t# Called every time the node is added to the scene.\n\t# Initialization here\n\tplayer_size = get_node(\"player\").get_texture().get_size()\n\tget_node(\"cl0/inventory\").hide()\n\t#get_node(\"cl0/inventory_button\").hide()\n\t#get_node(\"player\").show()\n\t\n\tset_fixed_process(true)\n\nfunc _fixed_process(delta):\n\thealth -= receiveDamage *delta\n\tif(receiveDamage > 0):\n\t\tprint(health)\n\nfunc _integrate_forces(s):\n\tvar speedup = 0\n\tvar damage = 0\n\tvar weaponrange = 0\n\tspeedup = get_node(\"cl0/inventory\").getStats()[0]\n\t#print (speedup)\n\tWALK_MAX_VELOCITY = START_WALK_MAX_VELOCITY + speedup * 20\n\t\n\t#print (WALK_MAX_VELOCITY)\n\t\n\tvar lv = s.get_linear_velocity()\n\tvar step = s.get_step()\n\t\n\t#Inputs\n\tvar move_left = Input.is_action_pressed(\"move_left\")\n\tvar move_right = Input.is_action_pressed(\"move_right\")\n\tvar move_up = Input.is_action_pressed(\"move_up\")\n\tvar move_down = Input.is_action_pressed(\"move_down\")\n\tvar shoot = Input.is_action_pressed(\"shoot\")\n\n\tvar player_rect = Rect2( get_node(\"player\").get_pos() - player_size*0.5, player_size )\n\tvar player_pos = get_node(\"player\").get_pos()\n\t\n\t#Shooting\n\tif(shoot and not shooting):\n\t\tshoot_time = 0\n\t\tvar bi = bullet.instance()\n\t\tvar pos = get_pos() + get_node(\"bullet_shoot\").get_pos()\n\t\t\n\t\tbi.set_pos(pos)\n\t\tget_parent().add_child(bi)\n\t\t\n\t\tbi.set_linear_velocity(direction * 320)\n\t\t#get_node(\"sprite/smoke\").set_emitting(true)\n\t\t#get_node(\"sound\").play(\"shoot\")\n\t\tPS2D.body_add_collision_exception(bi.get_rid(), get_rid()) # Make bullet and this not collide\n\telse:\n\t\tshoot_time += step\n\n\t# MOVING \n\tif (move_left and not move_right): #left\n\t\tif (lv.x > -WALK_MAX_VELOCITY):\n\t\t\tlv.x -= WALK_ACCEL*step\n\t\tdirection = Vector2(-1.0, 0.0)\n\telif (move_right and not move_left): #right\n\t\tif (lv.x < WALK_MAX_VELOCITY):\n\t\t\tlv.x += WALK_ACCEL*step\n\t\tdirection = Vector2(1.0, 0.0)\n\telif (move_down and not move_up): #down\n\t\tif (lv.y < WALK_MAX_VELOCITY):\n\t\t\tlv.y += WALK_ACCEL*step\n\t\tdirection = Vector2(0.0, 1.0)\n\telif (move_up and not move_down): #up\n\t\tif (lv.y > -WALK_MAX_VELOCITY):\n\t\t\tlv.y -= WALK_ACCEL*step\n\t\tdirection = Vector2(0.0, -1.0)\n\telse: #Getting the player to stand still again\n\t\t\tvar xv = abs(lv.x)\n\t\t\tvar yv = abs(lv.y)\n\t\t\t\n\t\t\txv -= WALK_DEACCEL*step\n\t\t\tif (xv < 0):\n\t\t\t\txv = 0\n\t\t\tlv.x = sign(lv.x)*xv\n\t\t\t\n\t\t\tyv -= WALK_DEACCEL*step\n\t\t\tif (yv < 0):\n\t\t\t\tyv = 0\n\t\t\tlv.y = sign(lv.y)*yv\n\t\t\t\n\tshooting = shoot\n\ts.set_linear_velocity(lv)\n\nfunc showInventory():\n\tget_node(\"cl0/inventory\").show()\n\nfunc _on_inventory_pressed():\n\t#get_node(\"cl0/inventory_button\").hide()\n\tshowInventory()\n\t#hide()\n\t#print(\"inventory\")\n\tpass # replace with function body\n\n\nfunc _on_player_body_enter( body ):\n\tprint(\"player collision\")\n\tpass # replace with function body\n\n\nfunc _on_Area2D_body_enter( body ):\n\tif(body.is_in_group(\"enemy\")):\n\t\tenemyCollision(body,1)\n\telif(body.is_in_group(\"container\")):\n\t\tshowInventory()\n\t\tget_node(\"cl0/inventory\").addRandomItem()\n\t\tbody.free()\n\tpass # replace with function body\n\nfunc _on_Area2D_body_exit( body ):\n\tif(body.is_in_group(\"enemy\")):\n\t\tenemyCollision(body,-1)\n\tpass # replace with function body\n\n#add = [1,-1]\nfunc enemyCollision(enemy,add):\n\treceiveDamage += enemy.getDamage() *add\n\n\nfunc _on_Area2D_area_enter( body ):\n\tif(body.is_in_group(\"enemy\")):\n\t\tenemyCollision(body,1)\n\telif(body.is_in_group(\"container\")):\n\t\tshowInventory()\n\t\tget_node(\"cl0/inventory\").addRandomItem()\n\t\tbody.free()\n\tpass # replace with function body\n" [node name="player" type="RigidBody2D"] @@ -115,7 +115,6 @@ drag_margin/bottom = 0.2 [node name="Area2D" type="Area2D" parent="."] -editor/display_folded = true input/pickable = true shapes/0/shape = SubResource( 1 ) shapes/0/transform = Matrix32( 3.25823, 0, 0, 3.24878, -555.629, -297.576 ) @@ -140,6 +139,8 @@ transform/pos = Vector2( -555.263, -297.351 ) [connection signal="pressed" from="cl0/inventory_button" to="." method="_on_inventory_pressed"] +[connection signal="area_enter" from="Area2D" to="." method="_on_Area2D_area_enter"] + [connection signal="body_enter" from="Area2D" to="." method="_on_Area2D_body_enter"] [connection signal="body_exit" from="Area2D" to="." method="_on_Area2D_body_exit"]