Skip to content

Commit

Permalink
it makes a red rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
trevyn committed Sep 15, 2024
1 parent 20fffe8 commit 10e5473
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 136 deletions.
122 changes: 2 additions & 120 deletions Godottest/res/node_2d.tscn
Original file line number Diff line number Diff line change
@@ -1,121 +1,3 @@
[gd_scene load_steps=12 format=3 uid="uid://pdkn35rn7rts"]
[gd_scene format=3 uid="uid://pdkn35rn7rts"]

[ext_resource type="Script" path="res://res/node_2d.gd" id="1_atssm"]
[ext_resource type="VideoStream" path="res://res/media/mainmenu-to-eyesclosedheadback.ogv" id="1_huc1b"]
[ext_resource type="VideoStream" path="res://res/media/mainmenu-from-walkin.ogv" id="1_ila7w"]
[ext_resource type="Script" path="res://res/node.gd" id="1_ypcjg"]
[ext_resource type="Script" path="res://res/vsp.gd" id="2_v4qc7"]
[ext_resource type="VideoStream" path="res://res/media/mainmenu-from-backofhead.ogv" id="5_3gsv2"]
[ext_resource type="VideoStream" path="res://res/media/mainmenu-to-lookdownandup.ogv" id="6_3pm7c"]
[ext_resource type="VideoStream" path="res://res/media/mainmenu-to-lookdownandshrug.ogv" id="7_g2nyq"]

[sub_resource type="Theme" id="Theme_pqtiu"]
default_font_size = 36

[sub_resource type="FontVariation" id="FontVariation_1deny"]
spacing_glyph = -3

[sub_resource type="Theme" id="Theme_norqa"]
default_font = SubResource("FontVariation_1deny")
default_font_size = 55

[node name="Node2D" type="Node2D"]

[node name="Node" type="Node" parent="."]
script = ExtResource("1_ypcjg")

[node name="VideoStreamPlayer6" type="VideoStreamPlayer" parent="Node"]
offset_right = 1280.0
offset_bottom = 768.0
stream = ExtResource("7_g2nyq")
volume_db = -80.0
script = ExtResource("2_v4qc7")
fadein = false

[node name="VideoStreamPlayer5" type="VideoStreamPlayer" parent="Node"]
offset_right = 1280.0
offset_bottom = 768.0
stream = ExtResource("5_3gsv2")
volume_db = -80.0
script = ExtResource("2_v4qc7")
fadeout = false

[node name="VideoStreamPlayer4" type="VideoStreamPlayer" parent="Node"]
offset_right = 1280.0
offset_bottom = 768.0
stream = ExtResource("6_3pm7c")
volume_db = -80.0
script = ExtResource("2_v4qc7")
fadein = false

[node name="VideoStreamPlayer3" type="VideoStreamPlayer" parent="Node"]
offset_right = 1280.0
offset_bottom = 768.0
stream = ExtResource("5_3gsv2")
volume_db = -80.0
script = ExtResource("2_v4qc7")
fadeout = false

[node name="VideoStreamPlayer2" type="VideoStreamPlayer" parent="Node"]
offset_right = 1280.0
offset_bottom = 768.0
stream = ExtResource("1_huc1b")
volume_db = -80.0
script = ExtResource("2_v4qc7")
fadein = false

[node name="VideoStreamPlayer" type="VideoStreamPlayer" parent="Node"]
offset_right = 1280.0
offset_bottom = 768.0
stream = ExtResource("1_ila7w")
volume_db = -80.0
script = ExtResource("2_v4qc7")
fadeout = false

[node name="Node2D" type="Node2D" parent="Node"]
script = ExtResource("1_atssm")

[node name="Label" type="Label" parent="Node/Node2D"]
offset_left = 724.0
offset_top = 374.0
offset_right = 956.0
offset_bottom = 424.0
theme = SubResource("Theme_pqtiu")
text = "⇒ New Game"
horizontal_alignment = 2

[node name="Label4" type="Label" parent="Node/Node2D"]
offset_left = 767.0
offset_top = 326.0
offset_right = 957.0
offset_bottom = 376.0
theme = SubResource("Theme_pqtiu")
text = "Continue"
horizontal_alignment = 2

[node name="Label5" type="Label" parent="Node/Node2D"]
offset_left = 444.0
offset_top = 87.0
offset_right = 962.0
offset_bottom = 186.0
theme = SubResource("Theme_norqa")
text = "dungeon game"
horizontal_alignment = 2

[node name="Label2" type="Label" parent="Node/Node2D"]
offset_left = 766.0
offset_top = 423.0
offset_right = 956.0
offset_bottom = 473.0
theme = SubResource("Theme_pqtiu")
text = "Settings"
horizontal_alignment = 2

[node name="Label3" type="Label" parent="Node/Node2D"]
offset_left = 763.0
offset_top = 473.0
offset_right = 953.0
offset_bottom = 523.0
theme = SubResource("Theme_pqtiu")
text = "Exit"
horizontal_alignment = 2
[node name="GameManager" type="GameManager"]
126 changes: 110 additions & 16 deletions deckbuilder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,127 @@
use godot::builtin::{Color, Vector2};
use godot::classes::{ColorRect, Node, Node2D};
use godot::prelude::*;

// #![allow(unused)]
fn main() {
struct MyExtension;
struct MyExtension;

#[gdextension]
unsafe impl ExtensionLibrary for MyExtension {}
}

use godot::classes::{INode, Node};
#[gdextension]
unsafe impl ExtensionLibrary for MyExtension {}

#[derive(GodotClass)]
#[class(base=Node)]
struct MyPlayer {
speed: f64,
angular_speed: f64,

pub struct GameManager {
#[base]
base: Base<Node>,
}

#[godot_api]
impl INode for MyPlayer {
impl INode for GameManager {
fn init(base: Base<Node>) -> Self {
godot_print!("Hello, worldd!"); // Prints to the Godot console
godot_print!("GameManager initialized!");
Self { base }
}

fn ready(&mut self) {
godot_print!("GameManager ready() called");
let my_player = MyPlayer::new_alloc();
godot_print!("MyPlayer allocated: {:?}", my_player);
self.base_mut()
.add_child(my_player.clone().upcast::<Node>());
godot_print!("MyPlayer added as child to GameManager");

let player_node = self.base().get_node_as::<MyPlayer>("MyPlayer");
godot_print!("MyPlayer found in scene tree: {:?}", player_node);
}
}

#[godot_api]
impl GameManager {
#[func]
fn create_player(&mut self) {
godot_print!("Creating new player");
let my_player = MyPlayer::new_alloc();
self.base_mut().add_child(my_player.upcast::<Node>());
godot_print!("New player created and added as child");
}
}
#[derive(GodotClass)]
#[class(base=Node2D)]
pub struct MyPlayer {
#[base]
base: Base<Node2D>,
speed: f32,
direction: Vector2,
shape: Gd<ColorRect>,
}

#[godot_api]
impl INode2D for MyPlayer {
fn init(base: Base<Node2D>) -> Self {
godot_print!("MyPlayer initialized!");
let mut rect = ColorRect::new_alloc();
rect.set_size(Vector2::new(50.0, 50.0));
rect.set_position(Vector2::new(-25.0, -25.0));
rect.set_color(Color::from_rgb(1.0, 0.0, 0.0)); // Red color
godot_print!(
"ColorRect created for MyPlayer: size={:?}, position={:?}, color={:?}",
rect.get_size(),
rect.get_position(),
rect.get_color()
);
Self {
speed: 400.0,
angular_speed: std::f64::consts::PI,
base,
speed: 100.0,
direction: Vector2::new(1.0, 1.0),
shape: rect,
}
}

fn ready(&mut self) {
godot_print!("MyPlayer ready called");
let shape_clone = self.shape.clone();
self.base_mut()
.add_child(shape_clone.clone().upcast::<Node>());
godot_print!("ColorRect added as child to MyPlayer");
let added_shape = self.base().get_node_as::<ColorRect>("ColorRect");
godot_print!("ColorRect found in scene tree: {:?}", added_shape);
}
}

#[godot_api]
impl MyPlayer {
#[func]
fn process(&mut self, delta: f64) {
self.move_shape(delta);
}

fn move_shape(&mut self, delta: f64) {
let mut position = self.base().get_global_position();
let old_position = position;
position += self.direction * self.speed * delta as f32;

// Bounce off screen edges
if let Some(viewport) = self.base().get_viewport() {
let size = viewport.get_visible_rect().size;
if position.x < 0.0 || position.x > size.x {
self.direction.x *= -1.0;
godot_print!(
"MyPlayer bounced horizontally. New direction: {:?}",
self.direction
);
}
if position.y < 0.0 || position.y > size.y {
self.direction.y *= -1.0;
godot_print!(
"MyPlayer bounced vertically. New direction: {:?}",
self.direction
);
}
}

self.base_mut().set_global_position(position);
if (position - old_position).length() > 1.0 {
// Only log if moved more than 1 unit
godot_print!("MyPlayer moved from {:?} to {:?}", old_position, position);
}
}
}
Expand Down

0 comments on commit 10e5473

Please sign in to comment.