diff --git a/client/.import/dirt.png-2d96e51b4345f83ec0fb0c1ed8bf2b89.md5 b/client/.import/dirt.png-2d96e51b4345f83ec0fb0c1ed8bf2b89.md5 new file mode 100644 index 0000000..d3c727a --- /dev/null +++ b/client/.import/dirt.png-2d96e51b4345f83ec0fb0c1ed8bf2b89.md5 @@ -0,0 +1,3 @@ +source_md5="ea3da320528c7d54640506628b12b80c" +dest_md5="f8c65d4e4cd80d79c48ff0c014cf0377" + diff --git a/client/.import/dirt.png-2d96e51b4345f83ec0fb0c1ed8bf2b89.stex b/client/.import/dirt.png-2d96e51b4345f83ec0fb0c1ed8bf2b89.stex new file mode 100644 index 0000000..e688d24 Binary files /dev/null and b/client/.import/dirt.png-2d96e51b4345f83ec0fb0c1ed8bf2b89.stex differ diff --git a/client/.import/grass.png-d6f949232ad65f7c5ac8611d6943aaf7.md5 b/client/.import/grass.png-d6f949232ad65f7c5ac8611d6943aaf7.md5 new file mode 100644 index 0000000..675b906 --- /dev/null +++ b/client/.import/grass.png-d6f949232ad65f7c5ac8611d6943aaf7.md5 @@ -0,0 +1,3 @@ +source_md5="b38dac28f777cd24a94ae764cf60780a" +dest_md5="8568362e706ffbdddbdcb895eaad16c8" + diff --git a/client/.import/grass.png-d6f949232ad65f7c5ac8611d6943aaf7.stex b/client/.import/grass.png-d6f949232ad65f7c5ac8611d6943aaf7.stex new file mode 100644 index 0000000..3f1b88d Binary files /dev/null and b/client/.import/grass.png-d6f949232ad65f7c5ac8611d6943aaf7.stex differ diff --git a/client/art/tiles/dirt.png b/client/art/tiles/dirt.png new file mode 100644 index 0000000..b850058 Binary files /dev/null and b/client/art/tiles/dirt.png differ diff --git a/client/art/tiles/dirt.png.import b/client/art/tiles/dirt.png.import new file mode 100644 index 0000000..16e9e0a --- /dev/null +++ b/client/art/tiles/dirt.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/dirt.png-2d96e51b4345f83ec0fb0c1ed8bf2b89.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://art/tiles/dirt.png" +dest_files=[ "res://.import/dirt.png-2d96e51b4345f83ec0fb0c1ed8bf2b89.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/client/art/tiles/grass.png b/client/art/tiles/grass.png new file mode 100644 index 0000000..7bae1a1 Binary files /dev/null and b/client/art/tiles/grass.png differ diff --git a/client/art/tiles/grass.png.import b/client/art/tiles/grass.png.import new file mode 100644 index 0000000..010a90c --- /dev/null +++ b/client/art/tiles/grass.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/grass.png-d6f949232ad65f7c5ac8611d6943aaf7.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://art/tiles/grass.png" +dest_files=[ "res://.import/grass.png-d6f949232ad65f7c5ac8611d6943aaf7.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=false +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/client/nodes/tileset.tres b/client/nodes/tileset.tres new file mode 100644 index 0000000..a5d8131 --- /dev/null +++ b/client/nodes/tileset.tres @@ -0,0 +1,43 @@ +[gd_resource type="TileSet" load_steps=3 format=2] + +[ext_resource path="res://art/tiles/grass.png" type="Texture" id=1] +[ext_resource path="res://art/tiles/dirt.png" type="Texture" id=2] + +[resource] +0/name = "grass.png 0" +0/texture = ExtResource( 1 ) +0/tex_offset = Vector2( 0, 0 ) +0/modulate = Color( 1, 1, 1, 1 ) +0/region = Rect2( 0, 0, 352, 160 ) +0/tile_mode = 1 +0/autotile/bitmask_mode = 1 +0/autotile/bitmask_flags = [ Vector2( 0, 0 ), 432, Vector2( 0, 1 ), 438, Vector2( 0, 2 ), 54, Vector2( 0, 3 ), 48, Vector2( 1, 0 ), 504, Vector2( 1, 1 ), 511, Vector2( 1, 2 ), 63, Vector2( 1, 3 ), 56, Vector2( 2, 0 ), 216, Vector2( 2, 1 ), 219, Vector2( 2, 2 ), 27, Vector2( 2, 3 ), 24, Vector2( 3, 0 ), 144, Vector2( 3, 1 ), 146, Vector2( 3, 2 ), 18, Vector2( 3, 3 ), 16, Vector2( 4, 0 ), 176, Vector2( 4, 1 ), 182, Vector2( 4, 2 ), 434, Vector2( 4, 3 ), 50, Vector2( 4, 4 ), 178, Vector2( 5, 0 ), 248, Vector2( 5, 1 ), 255, Vector2( 5, 2 ), 507, Vector2( 5, 3 ), 59, Vector2( 5, 4 ), 251, Vector2( 6, 0 ), 440, Vector2( 6, 1 ), 447, Vector2( 6, 2 ), 510, Vector2( 6, 3 ), 62, Vector2( 6, 4 ), 446, Vector2( 7, 0 ), 152, Vector2( 7, 1 ), 155, Vector2( 7, 2 ), 218, Vector2( 7, 3 ), 26, Vector2( 7, 4 ), 154, Vector2( 8, 0 ), 184, Vector2( 8, 1 ), 191, Vector2( 8, 2 ), 506, Vector2( 8, 3 ), 58, Vector2( 8, 4 ), 186, Vector2( 9, 0 ), 443, Vector2( 9, 1 ), 254, Vector2( 9, 2 ), 442, Vector2( 9, 3 ), 190, Vector2( 10, 2 ), 250, Vector2( 10, 3 ), 187 ] +0/autotile/icon_coordinate = Vector2( 0, 0 ) +0/autotile/tile_size = Vector2( 32, 32 ) +0/autotile/spacing = 0 +0/autotile/occluder_map = [ ] +0/autotile/navpoly_map = [ ] +0/autotile/priority_map = [ ] +0/autotile/z_index_map = [ ] +0/occluder_offset = Vector2( 0, 0 ) +0/navigation_offset = Vector2( 0, 0 ) +0/shape_offset = Vector2( 0, 0 ) +0/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 ) +0/shape_one_way = false +0/shape_one_way_margin = 0.0 +0/shapes = [ ] +0/z_index = 0 +1/name = "dirt.png 1" +1/texture = ExtResource( 2 ) +1/tex_offset = Vector2( 0, 0 ) +1/modulate = Color( 1, 1, 1, 1 ) +1/region = Rect2( 0, 0, 32, 32 ) +1/tile_mode = 0 +1/occluder_offset = Vector2( 0, 0 ) +1/navigation_offset = Vector2( 0, 0 ) +1/shape_offset = Vector2( 0, 0 ) +1/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 ) +1/shape_one_way = false +1/shape_one_way_margin = 0.0 +1/shapes = [ ] +1/z_index = 0 diff --git a/client/scenes/World.tscn b/client/scenes/World.tscn new file mode 100644 index 0000000..247e3eb --- /dev/null +++ b/client/scenes/World.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://nodes/tileset.tres" type="TileSet" id=1] +[ext_resource path="res://scripts/singletons/WorldManager.gd" type="Script" id=2] + +[node name="World" type="Node2D"] +script = ExtResource( 2 ) +tilemapPath = NodePath("TileMap") + +[node name="TileMap" type="TileMap" parent="."] +tile_set = ExtResource( 1 ) +cell_size = Vector2( 32, 32 ) +format = 1 +tile_data = PoolIntArray( 0, 0, 0, 1, 0, 1, 2, 0, 1, 3, 0, 1, 4, 0, 1, 5, 0, 1, 6, 0, 5, 7, 0, 196609, 8, 0, 196609, 9, 0, 6, 10, 0, 1, 11, 0, 1, 12, 0, 1, 13, 0, 1, 14, 0, 1, 15, 0, 2, 65536, 0, 65536, 65537, 0, 65537, 65538, 0, 65537, 65539, 0, 65537, 65540, 0, 65537, 65541, 0, 65537, 65542, 0, 65538, 65543, 1, 0, 65544, 1, 0, 65545, 0, 65536, 65546, 0, 65537, 65547, 0, 65537, 65548, 0, 65537, 65549, 0, 65537, 65550, 0, 65537, 65551, 0, 65538, 131072, 0, 65536, 131073, 0, 65537, 131074, 0, 65537, 131075, 0, 65537, 131076, 0, 65537, 131077, 0, 65537, 131078, 0, 65538, 131079, 1, 0, 131080, 1, 0, 131081, 0, 65536, 131082, 0, 65537, 131083, 0, 65537, 131084, 0, 65537, 131085, 0, 65537, 131086, 0, 65537, 131087, 0, 65538, 196608, 0, 65536, 196609, 0, 65537, 196610, 0, 65537, 196611, 0, 65537, 196612, 0, 65537, 196613, 0, 65537, 196614, 0, 65538, 196615, 1, 0, 196616, 1, 0, 196617, 0, 65536, 196618, 0, 65537, 196619, 0, 65537, 196620, 0, 65537, 196621, 0, 65537, 196622, 0, 65537, 196623, 0, 65538, 262144, 0, 65536, 262145, 0, 65537, 262146, 0, 65537, 262147, 0, 65537, 262148, 0, 65537, 262149, 0, 65537, 262150, 0, 65538, 262151, 1, 0, 262152, 1, 0, 262153, 0, 65536, 262154, 0, 65537, 262155, 0, 65537, 262156, 0, 65537, 262157, 0, 65537, 262158, 0, 65537, 262159, 0, 65538, 327680, 0, 65536, 327681, 0, 65537, 327682, 0, 65537, 327683, 0, 65537, 327684, 0, 65537, 327685, 0, 65537, 327686, 0, 65538, 327687, 1, 0, 327688, 1, 0, 327689, 0, 65536, 327690, 0, 65537, 327691, 0, 65537, 327692, 0, 65537, 327693, 0, 65537, 327694, 0, 65537, 327695, 0, 65538, 393216, 0, 65540, 393217, 0, 131073, 393218, 0, 131073, 393219, 0, 131073, 393220, 0, 131073, 393221, 0, 131073, 393222, 0, 131074, 393223, 1, 0, 393224, 1, 0, 393225, 0, 131072, 393226, 0, 131073, 393227, 0, 131073, 393228, 0, 131073, 393229, 0, 131073, 393230, 0, 131073, 393231, 0, 65543, 458752, 0, 65539, 458753, 1, 0, 458754, 1, 0, 458755, 1, 0, 458756, 1, 0, 458757, 1, 0, 458758, 1, 0, 458759, 1, 0, 458760, 1, 0, 458761, 1, 0, 458762, 1, 0, 458763, 1, 0, 458764, 1, 0, 458765, 1, 0, 458766, 1, 0, 458767, 0, 65539, 524288, 0, 65539, 524289, 1, 0, 524290, 1, 0, 524291, 1, 0, 524292, 1, 0, 524293, 1, 0, 524294, 1, 0, 524295, 1, 0, 524296, 1, 0, 524297, 1, 0, 524298, 1, 0, 524299, 1, 0, 524300, 1, 0, 524301, 1, 0, 524302, 1, 0, 524303, 0, 65539, 589824, 0, 131076, 589825, 0, 1, 589826, 0, 1, 589827, 0, 1, 589828, 0, 1, 589829, 0, 1, 589830, 0, 2, 589831, 1, 0, 589832, 1, 0, 589833, 0, 0, 589834, 0, 1, 589835, 0, 1, 589836, 0, 1, 589837, 0, 1, 589838, 0, 1, 589839, 0, 131079, 655360, 0, 65536, 655361, 0, 65537, 655362, 0, 65537, 655363, 0, 65537, 655364, 0, 65537, 655365, 0, 65537, 655366, 0, 65538, 655367, 1, 0, 655368, 1, 0, 655369, 0, 65536, 655370, 0, 65537, 655371, 0, 65537, 655372, 0, 65537, 655373, 0, 65537, 655374, 0, 65537, 655375, 0, 65538, 720896, 0, 65536, 720897, 0, 65537, 720898, 0, 65537, 720899, 0, 65537, 720900, 0, 65537, 720901, 0, 65537, 720902, 0, 65538, 720903, 1, 0, 720904, 1, 0, 720905, 0, 65536, 720906, 0, 65537, 720907, 0, 65537, 720908, 0, 65537, 720909, 0, 65537, 720910, 0, 65537, 720911, 0, 65538, 786432, 0, 65536, 786433, 0, 65537, 786434, 0, 65537, 786435, 0, 65537, 786436, 0, 65537, 786437, 0, 65537, 786438, 0, 65538, 786439, 1, 0, 786440, 1, 0, 786441, 0, 65536, 786442, 0, 65537, 786443, 0, 65537, 786444, 0, 65537, 786445, 0, 65537, 786446, 0, 65537, 786447, 0, 65538, 851968, 0, 65536, 851969, 0, 65537, 851970, 0, 65537, 851971, 0, 65537, 851972, 0, 65537, 851973, 0, 65537, 851974, 0, 65538, 851975, 1, 0, 851976, 1, 0, 851977, 0, 65536, 851978, 0, 65537, 851979, 0, 65537, 851980, 0, 65537, 851981, 0, 65537, 851982, 0, 65537, 851983, 0, 65538, 917504, 0, 65536, 917505, 0, 65537, 917506, 0, 65537, 917507, 0, 65537, 917508, 0, 65537, 917509, 0, 65537, 917510, 0, 65538, 917511, 1, 0, 917512, 1, 0, 917513, 0, 65536, 917514, 0, 65537, 917515, 0, 65537, 917516, 0, 65537, 917517, 0, 65537, 917518, 0, 65537, 917519, 0, 65538, 983040, 0, 131072, 983041, 0, 131073, 983042, 0, 131073, 983043, 0, 131073, 983044, 0, 131073, 983045, 0, 131073, 983046, 0, 196613, 983047, 0, 196609, 983048, 0, 196609, 983049, 0, 196614, 983050, 0, 131073, 983051, 0, 131073, 983052, 0, 131073, 983053, 0, 131073, 983054, 0, 131073, 983055, 0, 131074 ) diff --git a/client/scripts/menus/login_form.gd b/client/scripts/menus/login_form.gd index 06a3b83..3007593 100644 --- a/client/scripts/menus/login_form.gd +++ b/client/scripts/menus/login_form.gd @@ -40,7 +40,8 @@ func login(_text=""): display_message(error.message) else: display_message("Connected to server!", Color.green) - yield(ServerConnection.join_world_async(), "completed") + # Load World + get_tree().change_scene("res://scenes/World.tscn") func display_message(message="", color=Color.red): errorLabel.add_color_override("font_color", color) diff --git a/client/scripts/singletons/ServerConnection.gd b/client/scripts/singletons/ServerConnection.gd index 7902e18..de594d1 100644 --- a/client/scripts/singletons/ServerConnection.gd +++ b/client/scripts/singletons/ServerConnection.gd @@ -1,5 +1,7 @@ extends Node +signal tile_update(tile_data) + const KEY := "defaultkey" const SERVER_ENDPOINT := "nakama.cloudsumu.com" @@ -8,6 +10,10 @@ var _client : NakamaClient = Nakama.create_client(KEY, SERVER_ENDPOINT, 7350, "h var _socket : NakamaSocket var _precenses : Dictionary = {} +enum OPCODE { + tile_update = 1 +} + func authenticate_async(email : String, password : String) -> NakamaException: var result : NakamaException = null @@ -36,6 +42,7 @@ func connect_to_server_async() -> NakamaException: _socket = Nakama.create_socket_from(_client) var result : NakamaAsyncResult = yield(_socket.connect_async(_session), "completed") if not result.is_exception(): + _socket.connect("received_match_state", self, "_on_socket_received_match_state") _socket.connect("closed", self, "_on_socket_closed") return null return result.exception @@ -54,9 +61,15 @@ func join_world_async() -> Dictionary: for precense in match_join_result.presences: _precenses[precense.user_id] = precense - print("Currently connected: %s" % _precenses.size()) + print("Joined matched with %s other players!" % _precenses.size()) return _precenses func _on_socket_closed(): _socket = null + +func _on_socket_received_match_state(match_state: NakamaRTAPI.MatchData): + match match_state.op_code: + OPCODE.tile_update: + emit_signal("tile_update", JSON.parse(match_state.data).result) + diff --git a/client/scripts/singletons/WorldManager.gd b/client/scripts/singletons/WorldManager.gd new file mode 100644 index 0000000..4e09f8c --- /dev/null +++ b/client/scripts/singletons/WorldManager.gd @@ -0,0 +1,47 @@ +extends Node + +export(NodePath) var tilemapPath + +var tilemap : TileMap + +func _ready(): + + # Setup tilemap + tilemap = get_node(tilemapPath) + tilemap.clear() + + # Setup connections and join wolrd + ServerConnection.connect("tile_update", self, "on_tile_update") + yield(ServerConnection.join_world_async(), "completed") + +func on_tile_update(tile_data, update_bitmask=true): + print("Updating tilemap") + + var max_pos_x : int + var min_pos_x : int + var max_pos_y : int + var min_pos_y : int + + for x in tile_data: + + # Find max & min x + if not max_pos_x or max_pos_x > int(x): + max_pos_x = int(x) + if not min_pos_x or min_pos_x < int(x): + min_pos_x = int(x) + + for y in tile_data[x]: + + # Find max & min y + if not max_pos_y or max_pos_y > int(y): + max_pos_y = int(y) + if not min_pos_y or min_pos_y < int(y): + min_pos_y = int(y) + + # Update tile data + tilemap.set_cell(int(x),int(y), int(tile_data[x][y]), false, false, false, tilemap.get_cell_autotile_coord(int(x), int(y))) + + if update_bitmask: + tilemap.update_bitmask_region(Vector2(min_pos_x, min_pos_y), Vector2(max_pos_x, max_pos_y)) + + print("Update complete!") diff --git a/client/tests/test_worldmanager.gd b/client/tests/test_worldmanager.gd new file mode 100644 index 0000000..be8ed87 --- /dev/null +++ b/client/tests/test_worldmanager.gd @@ -0,0 +1,50 @@ +extends "res://addons/gut/test.gd" + +var world_manager = load("res://scripts/singletons/WorldManager.gd") + +func test_adding_tiles_to_map(): + # Configure world to have a + var world = world_manager.new() + world.tilemap = TileMap.new() + + world.on_tile_update({ + "0" : { + "0" : "0", + "1" : "0", + "2" : "0", + "3" : "1", + "4" : "0" + }, + "1" : { + "0" : "1" + } + }, false) + + assert_eq(world.tilemap.get_cell(0,0), 0) + assert_eq(world.tilemap.get_cell(0,1), 0) + assert_eq(world.tilemap.get_cell(0,2), 0) + assert_eq(world.tilemap.get_cell(0,3), 1) + assert_eq(world.tilemap.get_cell(0,4), 0) + assert_eq(world.tilemap.get_cell(1,0), 1) + + # Test Updates + world.on_tile_update({ + "0" : { + "1" : "0", + "2" : "1" + } + }, false) + + assert_eq(world.tilemap.get_cell(0,1), 0) + assert_eq(world.tilemap.get_cell(0,2), 1) + + # Test New Additions + world.on_tile_update({ + "1" : { + "6" : "0", + "7" : "1" + } + }, false) + + assert_eq(world.tilemap.get_cell(1,6), 0) + assert_eq(world.tilemap.get_cell(1,7), 1) diff --git a/server/plugin/control/control.go b/server/plugin/control/control.go index cd6dc87..3aceab0 100644 --- a/server/plugin/control/control.go +++ b/server/plugin/control/control.go @@ -3,29 +3,42 @@ package control import ( "context" "database/sql" - "github.com/heroiclabs/nakama-common/runtime" + "github.com/josephbmanley/family/server/plugin/gamemap" ) +// OpCode represents a enum for valid OpCodes +// used by the match logic +type OpCode int64 + +const ( + // OpCodeTileUpdate is used for tile updates + OpCodeTileUpdate = 1 +) + +// Match is the object registered +// as a runtime.Match interface type Match struct{} +// MatchState holds information that is passed between +// Nakama match methods type MatchState struct { presences map[string]runtime.Presence inputs map[string]string - positions map[string]string - jumps map[string]string - colors map[string]string + positions map[string]map[string]int names map[string]string + worldMap *gamemap.WorldMap } +// MatchInit is called when a new match is created func (m *Match) MatchInit(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, params map[string]interface{}) (interface{}, int, string) { + state := &MatchState{ presences: map[string]runtime.Presence{}, inputs: map[string]string{}, - positions: map[string]string{}, - jumps: map[string]string{}, - colors: map[string]string{}, + positions: map[string]map[string]int{}, names: map[string]string{}, + worldMap: gamemap.IntializeMap(), } tickRate := 10 label := "{\"name\": \"Game World\"}" @@ -33,8 +46,16 @@ func (m *Match) MatchInit(ctx context.Context, logger runtime.Logger, db *sql.DB return state, tickRate, label } +// MatchJoinAttempt is called when a player tried to join a match +// and validates their attempt func (m *Match) MatchJoinAttempt(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presence runtime.Presence, metadata map[string]string) (interface{}, bool, string) { - mState, _ := state.(*MatchState) + mState, ok := state.(*MatchState) + if !ok { + logger.Error("Invalid match state on join attempt!") + return state, false, "Invalid match state!" + } + + // Validate user is not already connected if _, ok := mState.presences[presence.GetUserId()]; ok { return mState, false, "User already logged in." } else { @@ -43,22 +64,52 @@ func (m *Match) MatchJoinAttempt(ctx context.Context, logger runtime.Logger, db } +// MatchJoin is called when a player successfully joins the match func (m *Match) MatchJoin(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presences []runtime.Presence) interface{} { - mState, _ := state.(*MatchState) + mState, ok := state.(*MatchState) + if !ok { + logger.Error("Invalid match state on join!") + return state + } + for _, precense := range presences { + + // Add presence to map mState.presences[precense.GetUserId()] = precense + + // Set player spawn pos + mState.positions[precense.GetUserId()] = map[string]int{"x": 16, "y": 16} + + mState.names[precense.GetUserId()] = "User" + + // Get intial tile data around player + if regionData, err := mState.worldMap.GetJSONRegionAround(16, 16, 8); err != nil { + logger.Error(err.Error()) + } else { + + // Broadcast tile data to client + if sendErr := dispatcher.BroadcastMessage(OpCodeTileUpdate, regionData, []runtime.Presence{precense}, precense, true); sendErr != nil { + logger.Error(sendErr.Error()) + } + } } return mState } +// MatchLeave is called when a player leaves the match func (m *Match) MatchLeave(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presences []runtime.Presence) interface{} { - mState, _ := state.(*MatchState) + mState, ok := state.(*MatchState) + if !ok { + logger.Error("Invalid match state on leave!") + return state + } for _, presence := range presences { delete(mState.presences, presence.GetUserId()) } return mState } +// MatchLoop is code that is executed every tick func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, messages []runtime.MatchData) interface{} { // Custom code to: // - Process the messages received. @@ -68,6 +119,7 @@ func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB return state } +// MatchTerminate is code that is executed when the match ends func (m *Match) MatchTerminate(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, graceSeconds int) interface{} { return state } diff --git a/server/plugin/gamemap/gamemap.go b/server/plugin/gamemap/gamemap.go new file mode 100644 index 0000000..55bc45a --- /dev/null +++ b/server/plugin/gamemap/gamemap.go @@ -0,0 +1,65 @@ +package gamemap + +import ( + "encoding/json" + "fmt" +) + +// WorldMap is the data structure used game world +type WorldMap struct { + data [64][64]int + max_x int + max_y int +} + +// GetTile method is used to grab a tile value with error checking +func (m WorldMap) GetTile(x int, y int) (int, error) { + if x > m.max_x || y > m.max_y { + return -1, fmt.Errorf("Map out of bounds error: %d, %d", x, y) + } + return m.data[x][y], nil +} + +// GetJSONRegion method returns a JSON object containing the tile values of everything +// within a given range +func (m WorldMap) GetJSONRegion(startX, endX, startY, endY int) ([]byte, error) { + regionMap := map[int]map[int]int{} + for x := startX; x < endX; x++ { + regionMap[x] = map[int]int{} + for y := startY; y < endY; y++ { + if result, err := m.GetTile(x, y); err != nil { + return nil, err + } else { + regionMap[x][y] = result + } + } + } + + jsonString, err := json.Marshal(regionMap) + return jsonString, err +} + +// GetJSONRegionAround returns a JSON object of tile data from a center point +func (m WorldMap) GetJSONRegionAround(centerX, centerY, regionRadius int) ([]byte, error) { + jsonString, err := m.GetJSONRegion(centerX-regionRadius, centerX+regionRadius, centerY-regionRadius, centerY+regionRadius) + return jsonString, err +} + +// IntializeMap is a method that helps easily +// generate WorldMap objects +func IntializeMap() *WorldMap { + worldMap := new(WorldMap) + worldMap.max_x = 64 + worldMap.max_y = 64 + worldMap.data = [64][64]int{} + for x := 0; x < worldMap.max_x; x++ { + for y := 0; y < worldMap.max_y; y++ { + worldMap.data[x][y] = 0 + } + } + + // Add dot at top left for testing + worldMap.data[0][0] = 1 + + return worldMap +} diff --git a/server/plugin/go.sum b/server/plugin/go.sum index 2015c67..85aeb17 100644 --- a/server/plugin/go.sum +++ b/server/plugin/go.sum @@ -15,6 +15,7 @@ github.com/heroiclabs/nakama-common v1.5.1 h1:ViCm9AvYYdQOCSKEa34SuSQ80JyZOHl6OD github.com/heroiclabs/nakama-common v1.5.1/go.mod h1:nZAXHdeo4SyPlCyf7pU9rCVizxEhBF74gt7teDe/EaQ= github.com/heroiclabs/nakama-common v1.7.2 h1:FQedePGCorBl3tXW4Ro8+XLGbEDQfGrT5Tb07j1UaLc= github.com/josephbmanley/family v0.0.0-20200815220504-0d9d05943cef h1:6oijVkew6eKI1fGE+YMaxmiNlp/hkN9wDpStoid9/ZI= +github.com/josephbmanley/family v0.0.0-20200816202226-abfb0f428423 h1:ynsJFMYkfs3JspzvLCfmPGJwdKY/4QeX457U0+y4J1I= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -42,6 +43,7 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/server/plugin/rpc/rpc.go b/server/plugin/rpc/rpc.go index 8635a53..ea5855d 100644 --- a/server/plugin/rpc/rpc.go +++ b/server/plugin/rpc/rpc.go @@ -9,27 +9,20 @@ import ( func getFirstWorld(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule) (string, error) { // List existing matches - // that have been 1 & 4 players + // that have been 1 & 32 players minSize := 1 - maxSize := 31 - //5, false, "", &minSize, &maxSize, "" + maxSize := 32 + + // Lists server authorative servers if matches, err := nk.MatchList(ctx, 1, true, "", &minSize, &maxSize, ""); err != nil { logger.Printf("Failed to list matches when grabing first world! Error: %v\n", err) return "", err } else { - - //For debug purposes - for _, match := range matches { - logger.Info("Found match with id: %s", match.GetMatchId()) - } - // If no matches exist, create one if len(matches) <= 0 { // Create match - //params := map[string]interface{}{} matchID, createErr := nk.MatchCreate(ctx, "control", map[string]interface{}{}) - //return nakama.match_create("world_control", {}) // Return if creation error if createErr != nil {