ask for tiletype

This commit is contained in:
zoe 2022-05-04 16:46:58 +02:00
parent 9d9348faca
commit 539727ca3f
11 changed files with 68 additions and 64 deletions

Binary file not shown.

View file

@ -1,5 +1,23 @@
extends Node
onready var server = $StateServer
export var xsize = 512
export var ysize = 512
export var zsize = 124
var Tilemap = preload("res://world/Tilemap.tscn")
var tilemaps = []
func _ready():
server.generate_world(1024,1024,512)
server.generate_world(xsize, ysize, zsize)
print(server.get_tile_at(xsize -1,ysize -1,zsize-1))
func _on_StateServer_request_full_reload():
respawn_tilemaps(zsize - 1)
func respawn_tilemaps(amount):
tilemaps = []
for tile in range(0, amount + 1):
add_child(Tilemap.instance())
tilemaps.push_back(tile)

10
godot/world/Tilemap.tscn Normal file
View file

@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://world/terrain/terrain.tres" type="TileSet" id=1]
[node name="Tilemap" type="TileMap"]
position = Vector2( -2, 0 )
mode = 1
tile_set = ExtResource( 1 )
cell_size = Vector2( 32, 16 )
format = 1

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

@ -0,0 +1,2 @@
extends Node

View file

@ -1,16 +0,0 @@
extends Node
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

View file

@ -1,13 +0,0 @@
extends TileMap
var width = 256
var height = 256
# Called when the node enters the scene tree for the first time.
func _ready():
fill()
func fill():
for x in width:
for y in height:
set_cell(x,y,0,false,false,false,Vector2(1,0))

View file

@ -1,25 +1,19 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=4 format=2]
[ext_resource path="res://world/terrain/terrain.tres" type="TileSet" id=1]
[ext_resource path="res://native/StateServer.tscn" type="PackedScene" id=2]
[ext_resource path="res://world/worldcam.gd" type="Script" id=3]
[ext_resource path="res://world/StateApi.gd" type="Script" id=5]
[node name="World" type="Node2D"]
[node name="StateApi" type="Node" parent="."]
script = ExtResource( 5 )
[node name="StateServer" parent="StateApi" instance=ExtResource( 2 )]
[node name="TileMap" type="TileMap" parent="."]
position = Vector2( -2, 0 )
mode = 1
tile_set = ExtResource( 1 )
cell_size = Vector2( 32, 16 )
format = 1
[node name="Main" type="Node2D"]
[node name="Worldcam" type="Camera2D" parent="."]
current = true
zoom = Vector2( 0.4, 0.4 )
script = ExtResource( 3 )
[node name="World" type="Node" parent="."]
script = ExtResource( 5 )
[node name="StateServer" parent="World" instance=ExtResource( 2 )]
[connection signal="request_full_reload" from="World/StateServer" to="World" method="_on_StateServer_request_full_reload"]

View file

@ -9,10 +9,11 @@ edition = "2021"
crate-type = ["cdylib"]
[dependencies]
gdnative = "0.10"
tokio = {version = "1.18.0", features = ["sync"]}
lazy_static = "1.4.0"
pathfinding = "3.0.12"
gdnative = {version = "0.10", features = ["async"]}
strum = { version = "0.24", features = ["derive"] }
# tokio = {version = "1.18.0", features = ["sync"]}
# lazy_static = "1.4.0"
# pathfinding = "3.0.12"
toml = "0.5.9"
[profile.dev.package."*"]

View file

@ -1,26 +1,21 @@
use gdnative::prelude::*;
use lazy_static::lazy_static;
use tokio::sync::RwLock;
mod saves;
mod world;
lazy_static! {
static ref WORLD: RwLock<Vec<Vec<Vec<u8>>>> = RwLock::new(vec![vec![vec![]]]);
}
#[derive(NativeClass)]
#[inherit(Node)]
#[register_with(Self::register)]
pub struct StateServer {
#[property]
a: u64,
world: world::World,
}
#[methods]
impl StateServer {
fn new(_owner: &Node) -> Self {
StateServer { a: 14 }
StateServer {
world: world::World::new(0, 0, 0),
}
}
#[export]
@ -39,9 +34,15 @@ impl StateServer {
}
#[export]
fn generate_world(&self, _owner: &Node, xsize: usize, ysize: usize, zsize: usize) {
let mut new_world = world::World::new(xsize, ysize, zsize);
new_world.generate();
fn generate_world(&mut self, _owner: &Node, xsize: usize, ysize: usize, zsize: usize) {
self.world = world::World::new(xsize, ysize, zsize);
self.world.generate();
_owner.emit_signal("request_full_reload", &[]);
}
#[export]
fn get_tile_at(&mut self, _owner: &Node, x: usize, y: usize, z: usize) -> &str {
self.world.get_tile_at(x, y, z).kind_to_string()
}
}
@ -53,5 +54,6 @@ impl StateServer {
.with_param("position", VariantType::Vector3)
.with_param("new_type", VariantType::GodotString)
.done();
builder.signal("request_full_reload").done();
}
}

View file

@ -1,5 +1,3 @@
use gdnative::prelude::*;
mod tiles;
pub struct World {
@ -10,6 +8,9 @@ pub struct World {
}
impl World {
pub fn get_tile_at(&self, x: usize, y: usize, z: usize) -> &tiles::Tile{
&self.tiles[x][y][z]
}
pub fn generate(&mut self) {
self.tiles = get_vec3(self.xsize, self.ysize, self.zsize);
for x in 0..self.xsize {

View file

@ -1,4 +1,6 @@
#[derive(Debug, Clone)]
use strum::AsRefStr;
#[derive(Debug, Clone, AsRefStr)]
pub enum Tiletypes {
Air,
Water,
@ -16,4 +18,7 @@ impl Tile {
pub fn new(kind: Tiletypes) -> Tile {
Tile { kind }
}
pub fn kind_to_string(&self) -> &str {
self.kind.as_ref()
}
}