Browse Source

changed physics and bullets

galo 1 year ago
parent
commit
46c0d26430
14 changed files with 166 additions and 37 deletions
  1. 28 2
      Client.gd
  2. 5 1
      Client.tscn
  3. 6 1
      Input.gd
  4. 12 1
      Server.gd
  5. 2 0
      Server.tscn
  6. 14 0
      bullet.gd
  7. 18 0
      bullet.tscn
  8. 0 1
      enemie/monster.tscn
  9. 1 1
      enemiessprite.gd
  10. 12 4
      main.tscn
  11. 24 26
      playerphysics.gd
  12. 1 0
      playersprite.tscn
  13. 20 0
      project.godot
  14. 23 0
      projectiles.gd

+ 28 - 2
Client.gd

@@ -50,6 +50,12 @@ remote func _On_Peer_Connected():
 	for x in current_clients:
 		gen_objs(playerphsyics,x,$players)
 		gen_objs(playerhud,x, $Hud/HBoxContainer)
+		if x == api.get_network_unique_id():
+			if $players.has_node(str(x)):
+				if not $players.get_node(str(x)).has_node('Camera2D'):
+					var cam2d = Camera2D.new()
+					cam2d.current = true
+					$players.get_node(str(x)).add_child(cam2d)
 	
 	if dummy:
 		rpc_id(1,'makedummmy')
@@ -87,8 +93,8 @@ func latency_test():
 remote func returnping(arg):
 	ping = (OS.get_system_time_msecs() - arg)
 
-func send_input(arg):
-	rpc_id(1,'input_handler',arg)
+func send_input(arg, arg2):
+	rpc_id(1,'input_handler',arg, arg2)
 
 remote func player_position(arg):
 	if not has_node("players"): return
@@ -112,6 +118,26 @@ func reconstruct_int16(bytetest):
 var argbuffer
 var posbuffer
 
+remote func projectiles_sync(arg):
+	if not has_node('projectiles'): return
+	for i in $projectiles.get_children():
+		i.hide()
+	
+	argbuffer = posbuffer
+	posbuffer = []
+	var sprite = []
+	for i in range(0,arg.size(),5):
+		var aux =  Vector2( reconstruct_int16([ arg[i] , arg[i+1]]), reconstruct_int16([ arg[i+2] , arg[i+3]]) )
+		posbuffer.append(aux)
+		sprite.append(arg[i+4])
+	
+	for i in posbuffer.size():
+		$projectiles.get_child(i).show()
+		$projectiles.get_child(i).global_position = posbuffer[i]
+	
+	for i in sprite.size():
+		$projectiles.get_child(i).texture = get_node('../Textures').dict[sprite[i]]
+
 remote func enemies_position_2(arg):
 	if not has_node('enemies'): return
 	for i in $enemies.get_children():

+ 5 - 1
Client.tscn

@@ -1,10 +1,11 @@
-[gd_scene load_steps=8 format=2]
+[gd_scene load_steps=9 format=2]
 
 [ext_resource path="res://Debug.tscn" type="PackedScene" id=1]
 [ext_resource path="res://PowerMenu.tscn" type="PackedScene" id=2]
 [ext_resource path="res://Client.gd" type="Script" id=3]
 [ext_resource path="res://Input.gd" type="Script" id=4]
 [ext_resource path="res://ping.gd" type="Script" id=5]
+[ext_resource path="res://projectiles.gd" type="Script" id=6]
 [ext_resource path="res://enemiessprite.gd" type="Script" id=7]
 [ext_resource path="res://font/tiny.tres" type="DynamicFont" id=8]
 
@@ -31,6 +32,9 @@ script = ExtResource( 4 )
 [node name="enemies" type="Node" parent="."]
 script = ExtResource( 7 )
 
+[node name="projectiles" type="Node" parent="."]
+script = ExtResource( 6 )
+
 [node name="Debug" parent="." instance=ExtResource( 1 )]
 
 [node name="Hud" type="CanvasLayer" parent="."]

+ 6 - 1
Input.gd

@@ -5,6 +5,7 @@ var enabled = false
 func _physics_process(delta):
 	if enabled:
 		var input = ''
+		var input2 = ''
 		if Input.is_action_pressed("ui_down"):
 			input =("ui_down")
 		if Input.is_action_pressed("ui_up"):
@@ -13,4 +14,8 @@ func _physics_process(delta):
 			input =("ui_left")
 		if Input.is_action_pressed("ui_right"):
 			input =("ui_right")
-		get_parent().send_input(input)
+		if Input.is_action_pressed("ui_attack"):
+			input2 =("ui_attack")
+		if Input.is_action_pressed("ui_jump"):
+			input =("ui_jump")
+		get_parent().send_input(input, input2)

+ 12 - 1
Server.gd

@@ -87,10 +87,11 @@ func disc(arg):
 remote func latency(arg):
 	rpc_id(api.get_rpc_sender_id(), 'returnping', arg)
 
-remote func input_handler(arg):
+remote func input_handler(arg,arg2):
 	var id = api.get_rpc_sender_id()
 	if $players.has_node(str(id)):
 		$players.get_node(str(id)).input = arg
+		$players.get_node(str(id)).input2 = arg2
 
 var tick = 0
 export var tickrate = 3
@@ -106,6 +107,16 @@ func _physics_process(delta):
 		animation_datachunk[i.name] = i.animation
 	rpc_unreliable_id(0, 'player_animation', animation_datachunk)
 	
+	
+	var projectile_datachunk = PoolByteArray()
+	for i in $projectiles.get_children():
+		var pos = i.global_position
+		projectile_datachunk.append_array( return_int16(pos.x) )
+		projectile_datachunk.append_array( return_int16(pos.y) )
+		var bytetest : PoolByteArray
+		bytetest.append(i.type)
+		projectile_datachunk.append_array( bytetest )
+	rpc_unreliable_id(0, 'projectiles_sync', projectile_datachunk)
 #	tick += 1
 #	if fmod(tick,tickrate) == 0:
 #		var enemies_datachunk = PoolByteArray()

+ 2 - 0
Server.tscn

@@ -10,3 +10,5 @@ tickrate = 4
 [node name="players" type="Node" parent="."]
 
 [node name="dmgbox" type="Node" parent="."]
+
+[node name="projectiles" type="Node" parent="."]

+ 14 - 0
bullet.gd

@@ -0,0 +1,14 @@
+extends KinematicBody2D
+
+var motion = Vector2.ZERO
+var type = 4
+
+#func _ready():
+#	print('bullet gen...')
+
+func _physics_process(delta):
+	var collide = move_and_collide(motion*delta)
+	if collide: queue_free()
+
+func _on_Timer_timeout():
+	queue_free()

+ 18 - 0
bullet.tscn

@@ -0,0 +1,18 @@
+[gd_scene load_steps=3 format=2]
+
+[ext_resource path="res://bullet.gd" type="Script" id=1]
+
+[sub_resource type="RectangleShape2D" id=1]
+extents = Vector2( 3, 3 )
+
+[node name="bullet" type="KinematicBody2D"]
+script = ExtResource( 1 )
+
+[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
+shape = SubResource( 1 )
+
+[node name="Timer" type="Timer" parent="."]
+wait_time = 3.0
+autostart = true
+
+[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]

+ 0 - 1
enemie/monster.tscn

@@ -1,4 +1,3 @@
 [gd_scene format=2]
 
 [node name="monster" type="Sprite"]
-visible = false

+ 1 - 1
enemiessprite.gd

@@ -5,7 +5,7 @@ var sprite = preload('res://enemie/monster.tscn')
 func _ready():
 	for i in 1024:
 		var add= sprite.instance()
-		add.texture = get_node('../../Textures').dict[1]
+#		add.texture = load('res://icon.png')
 		add.z_index = 1
 		add_child(add)
 #		add= sprite.instance()

+ 12 - 4
main.tscn

@@ -1,4 +1,4 @@
-[gd_scene load_steps=16 format=2]
+[gd_scene load_steps=17 format=2]
 
 [ext_resource path="res://Login.gd" type="Script" id=1]
 [ext_resource path="res://envarg.gd" type="Script" id=2]
@@ -8,6 +8,7 @@
 [ext_resource path="res://loginnetcode.gd" type="Script" id=6]
 [ext_resource path="res://BrowseColumn.tscn" type="PackedScene" id=7]
 [ext_resource path="res://tiles/testtile2.tres" type="TileSet" id=8]
+[ext_resource path="res://icon.png" type="Texture" id=9]
 [ext_resource path="res://art/Untitledcxz.png" type="Texture" id=10]
 [ext_resource path="res://Textures.gd" type="Script" id=11]
 
@@ -49,6 +50,7 @@ render_target_v_flip = true
 [node name="Goblin" type="AnimatedSprite" parent="Textures/Goblin1"]
 modulate = Color( 0, 0.811765, 1, 1 )
 frames = SubResource( 5 )
+frame = 3
 playing = true
 offset = Vector2( 32, 32 )
 
@@ -60,7 +62,6 @@ render_target_v_flip = true
 
 [node name="Goblin" type="AnimatedSprite" parent="Textures/Goblin2"]
 frames = SubResource( 5 )
-frame = 3
 speed_scale = 0.8
 playing = true
 offset = Vector2( 32, 32 )
@@ -74,7 +75,6 @@ render_target_v_flip = true
 [node name="Goblin" type="AnimatedSprite" parent="Textures/Goblin3"]
 modulate = Color( 1, 0, 0, 1 )
 frames = SubResource( 5 )
-frame = 3
 speed_scale = 1.2
 playing = true
 offset = Vector2( 32, 32 )
@@ -88,11 +88,19 @@ render_target_v_flip = true
 [node name="Goblin" type="AnimatedSprite" parent="Textures/Goblin4"]
 modulate = Color( 1, 0, 0.984314, 1 )
 frames = SubResource( 5 )
-frame = 1
 speed_scale = 0.9
 playing = true
 offset = Vector2( 32, 32 )
 
+[node name="Bullet" type="Viewport" parent="Textures"]
+size = Vector2( 8, 8 )
+own_world = true
+transparent_bg = true
+render_target_v_flip = true
+
+[node name="Sprite" type="Sprite" parent="Textures/Bullet"]
+texture = ExtResource( 9 )
+
 [node name="PostgreSQL" type="Node" parent="."]
 script = ExtResource( 3 )
 

+ 24 - 26
playerphysics.gd

@@ -1,6 +1,7 @@
 extends KinematicBody2D
 
 var input = ''
+var input2 = ''
 var animation = 'idle'
 var motion = Vector2()
 
@@ -16,8 +17,6 @@ var vida = 100
 
 func damage(arg, id):
 	vida += arg
-#	if vida <= 0: 
-#		vida = 0
 	get_node('../..').hp_update(vida, name)
 	if vida <= 0: 
 		queue_free()
@@ -28,17 +27,11 @@ func regenhp(arg):
 
 var sidebuffer = true
 
-#func melee():
-#	attack_buffers.melee += 1
-#	if fmod(attack_buffers.melee,45) == 0:
-#		var newdmg = damage.instance()
-#		newdmg.global_position = global_position + (Vector2(70,0) if sidebuffer else Vector2(-70,0))
-#		newdmg.attacker = name
-#		get_node('../../dmgbox').add_child(newdmg)
-#		get_node('../..').player_anim('melee',name)
-
 export var dmg = 35
+var waitframes_bullet = 12
+var aux_bullet = 12
 
+var bullet = preload('res://bullet.tscn')
 
 func melee():
 	attack_buffers.melee += 1
@@ -49,31 +42,36 @@ func melee():
 func dmg(arg = Vector2.ZERO):
 	var dir = Vector2.ZERO
 	dir.x = 80 if sidebuffer else -80
-#	get_node('../../../Server/enemies').damage_detection(global_position + dir+arg,dmg,name)
 
 func _physics_process(delta):
-#	get_node('../..').hp_update(vida, name)
-#	print('14')
 	if motion.x != 0: sidebuffer = motion.x > 0
-	for i in attacks:
-		funcref(self,i).call_func()
+	motion.y += 15
+	if motion.y < 0 and is_on_ceiling(): motion.y = 0
+	aux_bullet -= 1
+	match input2:
+		"ui_attack":
+			if aux_bullet <= 0:
+				var instance = bullet.instance()
+				instance.motion.x = 500 if sidebuffer else -500
+				instance.global_position = global_position
+				get_parent().get_parent().get_node('projectiles').add_child(instance)
+				aux_bullet = waitframes_bullet
+			
 	match input:
-		"ui_down":
-			motion.y = 100
-			animation = 'walk'
-		"ui_up":
-			motion.y = -100
-			animation = 'walk'
+		"ui_jump":
+			if is_on_floor():
+				motion.y = -400
+				animation = 'walk'
 		"ui_left":
-			motion.x = -100
+			motion.x = -200
 			animation = 'walk'
 		"ui_right":
-			motion.x = 100
+			motion.x = 200
 			animation = 'walk'
 		_:
-			motion = Vector2.ZERO
+			motion.x = 0
 			animation = 'idle'
-	move_and_slide(motion)
+	move_and_slide(motion,Vector2.UP)
 
 #power code
 

+ 1 - 0
playersprite.tscn

@@ -248,6 +248,7 @@ offset = Vector2( 3.72692, -21.0081 )
 script = SubResource( 28 )
 
 [node name="weapon" parent="." instance=ExtResource( 4 )]
+visible = false
 position = Vector2( 0, -8.0534 )
 scale = Vector2( 0.75, 0.75 )
 

+ 20 - 0
project.godot

@@ -36,6 +36,14 @@ window/stretch/aspect="keep"
 
 [input]
 
+ui_accept={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777221,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
+, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777222,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
+, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
+, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
+ ]
+}
 w={
 "deadzone": 0.5,
 "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
@@ -61,6 +69,18 @@ f3={
 "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777246,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
  ]
 }
+ui_attack={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":88,"unicode":0,"echo":false,"script":null)
+, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":2,"pressure":0.0,"pressed":false,"script":null)
+ ]
+}
+ui_jump={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":32,"unicode":0,"echo":false,"script":null)
+, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null)
+ ]
+}
 
 [layer_names]
 

+ 23 - 0
projectiles.gd

@@ -0,0 +1,23 @@
+extends Node
+
+var sprite = preload('res://enemie/monster.tscn')
+
+func _ready():
+	for i in 1024:
+		var add= sprite.instance()
+#		add.texture = get_node('../../Textures').dict[1]
+		add.z_index = 1
+		add_child(add)
+#		add= sprite.instance()
+#		add.texture = get_node('../../Textures').dict['Goblin2']
+#		add.z_index = 2
+#		add_child(add)
+#		add= sprite.instance()
+#		add.texture = get_node('../../Textures').dict['Goblin3']
+#		add.z_index = 3
+#		add_child(add)
+#		add= sprite.instance()
+#		add.texture = get_node('../../Textures').dict['Goblin4']
+#		add.z_index = 4
+#		add_child(add)
+	pass # Replace with function body.