diff --git a/client/nodes/entities/player.tscn b/client/nodes/entities/player.tscn index 240ff1e..92b8bd6 100644 --- a/client/nodes/entities/player.tscn +++ b/client/nodes/entities/player.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://assets/images/character/iron_player.png" type="Texture" id=1] [ext_resource path="res://scripts/entities/Player.gd" type="Script" id=2] +[ext_resource path="res://scripts/entities/Camera.gd" type="Script" id=3] [sub_resource type="RectangleShape2D" id=1] @@ -36,3 +37,4 @@ __meta__ = { [node name="Camera" type="Camera2D" parent="."] zoom = Vector2( 0.5, 0.5 ) +script = ExtResource( 3 ) diff --git a/client/scripts/entities/Camera.gd b/client/scripts/entities/Camera.gd new file mode 100644 index 0000000..245178a --- /dev/null +++ b/client/scripts/entities/Camera.gd @@ -0,0 +1,35 @@ +extends Camera2D + +#-------------# +# Camera Zoom # +#-------------# +export var zoomDecelration = 4 +export var zoomCapSpeed = 0.125 + +export var minZoom = 0.25 +export var maxZoom = 0.5 +var zoomChange = 0 + +func _ready(): + zoom.x = (minZoom + maxZoom) / 2 + zoom.y = (minZoom + maxZoom) / 2 + +func _process(delta): + CameraZoom(delta) + +func _input(event): + if event is InputEventMouseButton: + if event.is_pressed(): + if event.button_index == BUTTON_WHEEL_UP: + zoomChange = -zoomCapSpeed + if event.button_index == BUTTON_WHEEL_DOWN: + zoomChange = zoomCapSpeed + +func CameraZoom(delta): + if(zoomChange > 0): + zoomChange = clamp(zoomChange - zoomDecelration * delta, 0, zoomCapSpeed) + elif(zoomChange < 0): + zoomChange = clamp(zoomChange + zoomDecelration * delta, -zoomCapSpeed, 0) + + zoom.x = clamp(zoom.x + zoomChange, minZoom, maxZoom) + zoom.y = clamp(zoom.y + zoomChange, minZoom, maxZoom) diff --git a/client/scripts/network/NetworkManager.gd b/client/scripts/network/NetworkManager.gd index cee46a5..0b93a5a 100644 --- a/client/scripts/network/NetworkManager.gd +++ b/client/scripts/network/NetworkManager.gd @@ -23,6 +23,7 @@ var packetQueue = [] var error_info = "" var world_data : String = "" +var last_move_time = null var connection_timer : Timer @@ -111,9 +112,12 @@ func send_packet(packet : PoolByteArray, channel = 0, pck_type = GDNetMessage.RE packetQueue.append({'channel':channel, 'packet' : packet, 'type' : pck_type}) func move_player(x,y): - var pckt : PoolByteArray = ("3|" + str(x) + "," + str(y)).to_ascii() + if last_move_time == null || OS.get_ticks_msec() - last_move_time > 50: + + var pckt : PoolByteArray = ("3|" + str(x) + "," + str(y)).to_ascii() - send_packet(pckt, 0, GDNetMessage.SEQUENCED) + send_packet(pckt, 0, GDNetMessage.SEQUENCED) + last_move_time = OS.get_ticks_msec() func _process(delta): process_events() diff --git a/server/console.hpp b/server/console.hpp index c0d7184..1320fbb 100644 --- a/server/console.hpp +++ b/server/console.hpp @@ -32,9 +32,25 @@ void * console_logic(void *) { game_is_running = false; } + else if (input_string.length() > 2 && input_string.substr(0,3) == "say") + { + if(input_string.length() > 4) + { + gameserver::BroadcastMessage("Server: " + input_string.substr(4)); + } + else + { + std::cout << "Must pass a valid message!" << std::endl; + } + + } else { - std::cout << "Invalid console command!" << std::endl; + std::cout << std::endl + << "Invalid console command!" << std::endl + << "Valid commands are:" << std::endl + << "stop" << std::endl + << "say [message]" << std::endl; } } } diff --git a/server/gameserver.hpp b/server/gameserver.hpp index 178673e..27bf2aa 100644 --- a/server/gameserver.hpp +++ b/server/gameserver.hpp @@ -121,6 +121,37 @@ namespace gameserver } } + void SendPlayerMessage(ENetPeer* peer, std::string message) + { + const char* data = message.c_str(); + + ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(peer, 1, packet); + } + + bool SendPlayerMessage(std::string username, std::string message) + { + for(int i = 0; i < MAX_PLAYERS; i++) + { + if(usernames[i] == username) + { + SendPlayerMessage(&(server->peers[i]), message); + return true; + } + } + return false; + } + + void BroadcastMessage(std::string message) + { + const char* data = message.c_str(); + + std::cout << data << std::endl; + + ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); + enet_host_broadcast(server, 1, packet); + } + void ProcessChatMessage(ENetEvent* event) { int peer_id = event->peer -> incomingPeerID; @@ -131,12 +162,8 @@ namespace gameserver std::string chat_message; std::getline(ss, chat_message, '\n'); std::string resp = "<" + usernames[peer_id] + "> " + chat_message; - const char* data = resp.c_str(); + BroadcastMessage(resp); - std::cout << data << std::endl; - - ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); - enet_host_broadcast(server, 1, packet); } } @@ -184,7 +211,7 @@ namespace gameserver { while(game_is_running) { - usleep(50000); + usleep(50000*3); std::string data_string = "2|"+gamemap.world_tick(); const char* data = data_string.c_str(); ENetPacket* packet = enet_packet_create(data, strlen(data), ENET_PACKET_FLAG_UNSEQUENCED);