Compare commits

...

4 commits

Author SHA1 Message Date
zoe
5b58f246a0 ive decided its fine if the state server is public 2022-05-08 17:56:37 +02:00
zoe
74e9d94977 dskfjkdsf 2022-05-08 17:05:33 +02:00
zoe
59ddadb46a clean up a bit 2022-05-08 16:45:53 +02:00
zoe
3c5bbd204e internally use enum instead of u16 2022-05-08 14:09:11 +02:00
16 changed files with 82 additions and 56 deletions

View file

@ -1,3 +1,3 @@
source_md5="72b25c45d4743c578ac08e56b209be81"
dest_md5="73042650bec74eec84c50b98d4bafc70"
source_md5="d864960022980c4270042a1ed80b9ac4"
dest_md5="5f766eea69301dc969f05fac899cb4cb"

Binary file not shown.

View file

@ -11,9 +11,13 @@ config_version=4
[application]
config/name="routes"
run/main_scene="res://world/Main.tscn"
run/main_scene="res://world/World.tscn"
config/icon="res://icon.png"
[autoload]
StateServer="*res://native/StateServer.tscn"
[importer_defaults]
texture={

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.8 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

8
godot/world/Cam.tscn Normal file
View file

@ -0,0 +1,8 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://world/worldcam.gd" type="Script" id=1]
[node name="Worldcam" type="Camera2D"]
current = true
zoom = Vector2( 0.4, 0.4 )
script = ExtResource( 1 )

View file

@ -1,13 +1,10 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://world/worldcam.gd" type="Script" id=1]
[ext_resource path="res://world/Cam.tscn" type="PackedScene" id=1]
[ext_resource path="res://world/World.tscn" type="PackedScene" id=2]
[node name="Main" type="Node2D"]
[node name="Worldcam" type="Camera2D" parent="."]
current = true
zoom = Vector2( 0.4, 0.4 )
script = ExtResource( 1 )
[node name="Worldcam" parent="." instance=ExtResource( 1 )]
[node name="World" parent="." instance=ExtResource( 2 )]

View file

@ -1,37 +1,13 @@
extends Node
onready var server = $StateServer
export var xsize = 256
export var ysize = 256
export var zsize = 16
var Tilemap = preload("res://world/Tilemap.tscn")
var tilemaps = []
export var zsize = 6
func _ready():
server.generate_world(xsize, ysize, zsize)
print(server.get_tile_at(0,0,0))
StateServer.generate_world(xsize, ysize, zsize)
print(StateServer.get_tile_at(0,0,0))
func respawn_tilemaps(amount: int):
tilemaps = []
for i in range(0, amount + 1):
var map = Tilemap.instance()
map.position.y -= i * map.cell_size.y
map.z_index = i
add_child(map)
tilemaps.push_back(map)
$Tilemaps.respawn_tilemaps(amount)
func get_tile_at(pos: Vector3) -> String:
return server.get_tile_at(int(pos.x), int(pos.y), int(pos.z))
func update_tiles(tile_positions: PoolVector3Array):
for tile in tile_positions:
tilemaps[tile.z].set_tile_graphics(Vector2(tile.x, tile.y),
get_tile_at(tile))
func _on_StateServer_request_init():
respawn_tilemaps(zsize - 1)
func _on_StateServer_changed_tiletypes(tile_positions: PoolVector3Array):
call_deferred("update_tiles", tile_positions)

View file

@ -1,4 +1,8 @@
extends TileMap
func set_tile_graphics(pos: Vector2, id: int):
set_cell(pos.x,pos.y,0,false,false,false,Vector2(1,2))
var flip = true
set_cell(pos.x,pos.y,0,false,false,false,get_atlas_vec_for_id(id))
func get_atlas_vec_for_id(id: int) -> Vector2:
return Vector2(randi() % 4, id)

View file

@ -4,11 +4,11 @@
[ext_resource path="res://world/Tilemap.gd" type="Script" id=2]
[node name="Tilemap" type="TileMap"]
process_priority = 124
position = Vector2( -2, 0 )
mode = 1
tile_set = ExtResource( 1 )
cell_size = Vector2( 32, 16 )
cell_tile_origin = 1
collision_layer = 0
collision_mask = 0
format = 1

31
godot/world/Tilemaps.gd Normal file
View file

@ -0,0 +1,31 @@
extends Node
var Tilemap = preload("res://world/Tilemap.tscn")
var tilemaps = []
func _ready():
StateServer.connect("changed_tiletypes", self, "_on_StateServer_changed_tiletypes")
StateServer.connect("request_init", self, "_on_StateServer_request_init")
func respawn_tilemaps():
tilemaps = []
for i in range(0, StateServer.get_world_size().z):
var map = Tilemap.instance()
map.position.y -= i * map.cell_size.y
map.z_index = i
add_child(map)
tilemaps.push_back(map)
func get_tile_at(pos: Vector3) -> String:
return StateServer.get_tile_at(int(pos.x), int(pos.y), int(pos.z))
func update_tiles(tile_positions: PoolVector3Array):
for tile in tile_positions:
tilemaps[tile.z].set_tile_graphics(Vector2(tile.x, tile.y),
get_tile_at(tile))
func _on_StateServer_request_init():
call_deferred("respawn_tilemaps")
func _on_StateServer_changed_tiletypes(tile_positions: PoolVector3Array):
call_deferred("update_tiles", tile_positions)

View file

@ -1,12 +1,13 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=4 format=2]
[ext_resource path="res://world/StateApi.gd" type="Script" id=1]
[ext_resource path="res://native/StateServer.tscn" type="PackedScene" id=3]
[ext_resource path="res://world/Cam.tscn" type="PackedScene" id=2]
[ext_resource path="res://world/Tilemaps.gd" type="Script" id=4]
[node name="World" type="Node"]
script = ExtResource( 1 )
[node name="StateServer" parent="." instance=ExtResource( 3 )]
[node name="Worldcam" parent="." instance=ExtResource( 2 )]
[connection signal="changed_tiletypes" from="StateServer" to="." method="_on_StateServer_changed_tiletypes"]
[connection signal="request_init" from="StateServer" to="." method="_on_StateServer_request_init"]
[node name="Tilemaps" type="Node" parent="."]
script = ExtResource( 4 )

Binary file not shown.

View file

@ -39,6 +39,11 @@ impl StateServer {
_owner.emit_signal("changed_tiletypes", &[Variant::new(&w)]);
}
#[export]
fn get_world_size(&self, _ownser: &Node) -> Vector3 {
Vector3::new(self.world.xsize as f32, self.world.ysize as f32, self.world.zsize as f32)
}
#[export]
fn get_tile_at(&self, _owner: &Node, x: usize, y: usize, z: usize) -> u16 {
self.world.get_tile_at(x, y, z)

View file

@ -3,16 +3,16 @@ use gdnative::prelude::*;
mod tiles;
pub struct World {
xsize: usize,
ysize: usize,
zsize: usize,
tiles: Vec<Vec<Vec<u16>>>,
pub xsize: usize,
pub ysize: usize,
pub zsize: usize,
tiles: Vec<Vec<Vec<tiles::Tiletypes>>>,
}
impl World {
pub fn get_tile_at(&self, x: usize, y: usize, z: usize) -> u16{
//TODO: error handling, or maybe just do that in godot
self.tiles[x][y][z]
self.tiles[x][y][z] as u16
}
pub fn generate(&mut self) -> Vector3Array{
self.tiles = get_vec3(self.xsize, self.ysize, self.zsize);
@ -21,7 +21,7 @@ impl World {
for y in 0..self.ysize {
for z in 0..self.zsize {
ret.push(Vector3::new(x as f32, y as f32, z as f32));
self.tiles[x][y][z] = tiles::Tiletypes::Dirt as u16;
self.tiles[x][y][z] = tiles::Tiletypes::Grass;
}
}
}
@ -37,14 +37,14 @@ impl World {
}
}
fn get_vec3(xsize: usize, ysize: usize, zsize: usize) -> Vec<Vec<Vec<u16>>> {
let mut zvec: Vec<u16> = Vec::with_capacity(zsize);
zvec.resize(zsize, 0);
fn get_vec3(xsize: usize, ysize: usize, zsize: usize) -> Vec<Vec<Vec<tiles::Tiletypes>>> {
let mut zvec: Vec<tiles::Tiletypes> = Vec::with_capacity(zsize);
zvec.resize(zsize, tiles::Tiletypes::Air);
let mut yvec: Vec<Vec<u16>> = Vec::with_capacity(ysize);
let mut yvec: Vec<Vec<tiles::Tiletypes>> = Vec::with_capacity(ysize);
yvec.resize(ysize, zvec.clone());
let mut xvec: Vec<Vec<Vec<u16>>> = Vec::with_capacity(xsize);
let mut xvec: Vec<Vec<Vec<tiles::Tiletypes>>> = Vec::with_capacity(xsize);
xvec.resize(ysize, yvec.clone());
yvec.push(zvec);

View file

@ -1,8 +1,8 @@
use derive_builder::Builder;
use strum::{AsRefStr, EnumDiscriminants, EnumIter, FromRepr};
#[derive(AsRefStr, EnumIter, FromRepr, EnumDiscriminants, Clone)]
#[repr(u16)]
#[derive(AsRefStr, EnumIter, FromRepr, EnumDiscriminants, Clone, Copy)]
#[repr(u8)]
pub enum Tiletypes {
Air,
Water,