game#4 commit

This commit is contained in:
Kristofers Solo
2021-10-04 21:29:45 +03:00
parent a9313ed43b
commit 37b92eb551
32 changed files with 236 additions and 371 deletions

11
Game #4/code/global.gd Normal file
View File

@@ -0,0 +1,11 @@
extends Node
func instance_node_at_location(node: Object, parent: Object, location: Vector2) -> Object:
var node_instance = instance_node(node, parent)
node_instance.global_position = location
return node_instance
func instance_node(node: Object, parent: Object) -> Object:
var node_instance = node.instance()
parent.add_child(node_instance)
return node_instance

41
Game #4/code/network.gd Normal file
View File

@@ -0,0 +1,41 @@
extends Node
const DEFAULT_PORT = 28960
const MAX_CLIENTS = 3
var server = null
var client = null
var ip_address = ""
func _ready() -> void:
if OS.get_name() == "Windows":
ip_address = IP.get_local_addresses()[3]
elif OS.get_name() == "Android":
ip_address = IP.get_local_addresses()[0]
else:
ip_address = IP.get_local_addresses()[3]
for ip in IP.get_local_addresses():
if ip.begins_with("192.168.") and not ip.ends_with(".1"):
ip_address = ip
get_tree().connect("connected_to_server", self, "_connected_to_server")
get_tree().connect("server_disconnected", self, "_server_disconnected")
func create_server() -> void:
server = NetworkedMultiplayerENet.new()
server.create_server(DEFAULT_PORT, MAX_CLIENTS)
get_tree().set_network_peer(server)
func join_server() -> void:
client = NetworkedMultiplayerENet.new()
client.create_client(ip_address, DEFAULT_PORT)
get_tree().set_network_peer(client)
func _connected_to_server() -> void:
print("Successfully connected to the server")
func _server_disconnected() -> void:
print("Disconnected from the server")

View File

@@ -0,0 +1,49 @@
extends Control
var player = load("res://scenes/player.tscn")
onready var multiplayer_config_ui = $multiplayer_configure
onready var server_ip_address = $multiplayer_configure/server_ip_address
onready var device_ip_address = $CanvasLayer/device_ip_address
func _ready():
get_tree().connect("network_peer_connected", self, "_player_connected")
get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
get_tree().connect("connected_to_server", self, "_connected_to_server")
device_ip_address.text = Network.ip_address
func _player_connected(id) -> void:
print("Player " + str(id) + " has connected")
instance_player(id)
func _player_disconnected(id) -> void:
print("Player " + str(id) + " has disconnected")
if Players.has_node(str(id)):
Players.get_node(str(id)).queue_free()
func _on_create_server_pressed():
multiplayer_config_ui.hide()
Network.create_server()
instance_player(get_tree().get_network_unique_id())
func _on_join_server_pressed():
if server_ip_address.text != "":
multiplayer_config_ui.hide()
Network.ip_address = server_ip_address.text
Network.join_server()
func _connected_to_server() -> void:
yield(get_tree().create_timer(0.1), "timeout")
instance_player(get_tree().get_network_unique_id())
func instance_player(id) -> void:
var player_instance = Global.instance_node_at_location(player, Players, Vector2(rand_range(0, 1920), rand_range(0, 1080)))
player_instance.name = str(id)
player_instance.set_network_master(id)

88
Game #4/code/player.gd Normal file
View File

@@ -0,0 +1,88 @@
extends KinematicBody2D
const JUMP_FORCE = 500
const GRAVITY = 700
const MAX_SPEED = 10000
var speed = 15
var velocity = Vector2()
var fly = false
puppet var puppet_position = Vector2(0, 0) setget puppet_position_set
puppet var puppet_velocity = Vector2()
puppet var puppet_rotaion = 0
onready var tween = $Tween
func _physics_process(delta):
if is_network_master():
mode_switch(delta)
screen_wrap()
func mode_switch(delta):
if Input.is_action_just_pressed("mode_switch") and fly == false:
fly = true
elif Input.is_action_just_pressed("mode_switch") and fly == true:
fly = false
if fly == false:
movement(delta)
elif fly == true:
flying()
func movement(delta):
if Input.is_action_just_pressed("stop"):
velocity.x = 0
velocity.y = 0
if Input.is_action_pressed("move_left"):
if velocity.x > -MAX_SPEED:
velocity.x -= speed
elif Input.is_action_pressed("move_right"):
if velocity.x < MAX_SPEED:
velocity.x += speed
if !Input.is_action_pressed("move_left") and !Input.is_action_pressed("move_right") and is_on_floor():
velocity.x = 0
velocity.y += GRAVITY * delta
if Input.is_action_pressed("jump") and is_on_floor():
velocity.y -= JUMP_FORCE
velocity = move_and_slide(velocity, Vector2.UP)
func flying():
speed = 1000
var velocity = Vector2()
if Input.is_action_pressed("move_right"):
velocity.x += 1
if Input.is_action_pressed("move_left"):
velocity.x -= 1
if Input.is_action_pressed("move_down"):
velocity.y += 1
if Input.is_action_pressed("move_up"):
velocity.y -= 1
velocity = velocity.normalized() * speed
velocity = move_and_slide(velocity)
print(velocity)
func screen_wrap():
if position.x <= -10:
position.x = get_viewport_rect().size.x
if position.x >= get_viewport_rect().size.x + 10:
position.x = 0
func puppet_position_set(new_value) -> void:
puppet_position = new_value
tween.inturpolate_polarity(self, "global_position", global_position, puppet_position, 0.1)
tween.start()
func _on_network_tick_rate_timeout():
if is_network_master():
rset_unreliable("puppet_position", global_position)
rset_unreliable("puppet_velocity", velocity)