Skip to content

Commit

Permalink
add player movement
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Jun 1, 2024
1 parent 394f1db commit 55adc99
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Zenith

A indie fan game developed in godot engine
A indie fan game about undertale and terraria, developed in godot engine

## Build Guide

Expand Down
1 change: 1 addition & 0 deletions gdrust/src/fight_items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod block;
16 changes: 16 additions & 0 deletions gdrust/src/fight_items/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use godot::prelude::*;
use godot::engine::{Node2D, INode2D};

#[derive(GodotClass)]
#[class(base = Node2D)]
struct BlockDrawer {
base: Base<Node2D>
}

#[godot_api]
impl INode2D for BlockDrawer {
fn init(base: Base<Node2D>) -> BlockDrawer {
// godot_print!("BlockDrawer created from Godot Rust");
Self { base }
}
}
40 changes: 3 additions & 37 deletions gdrust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
use godot::engine::{ISprite2D, Sprite2D};
mod fight_items;
mod player;

use godot::prelude::*;

struct GdExtension;

#[gdextension()]
unsafe impl ExtensionLibrary for GdExtension {}

#[derive(GodotClass)]
#[class(base = Sprite2D)]
struct Player {
base: Base<Sprite2D>,
}

#[godot_api()]
impl ISprite2D for Player {
fn init(base: Base<Sprite2D>) -> Player {
godot_print!("Player created from Godot Rust");
Self { base }
}

fn process(&mut self, delta: f64) {
let mut vel = Vector2::ZERO;
let input_obj = Input::singleton();
if input_obj.is_action_pressed("move_left".into()) {
vel.x -= 1.0;
}
if input_obj.is_action_pressed("move_right".into()) {
vel.x += 1.0;
}
if input_obj.is_action_pressed("move_up".into()) {
vel.y -= 1.0;
}
if input_obj.is_action_pressed("move_down".into()) {
vel.y += 1.0;
}
}
}

#[godot_api]
impl Player {
#[constant]
const SPEED: i32 = 50;
}
46 changes: 46 additions & 0 deletions gdrust/src/player.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use godot::prelude::*;
use godot::engine::{Area2D, IArea2D};

#[derive(GodotClass)]
#[class(base = Area2D)]
struct Player {
base: Base<Area2D>,
}

#[godot_api()]
impl IArea2D for Player {
fn init(base: Base<Area2D>) -> Player {
godot_print!("Player created from Godot Rust");
Self { base }
}

fn physics_process(&mut self, delta: f64) {
let mut vel = Vector2::ZERO;
let input_obj = Input::singleton();
if input_obj.is_action_pressed("move_left".into()) {
vel.x -= 1.0;
}
if input_obj.is_action_pressed("move_right".into()) {
vel.x += 1.0;
}
if input_obj.is_action_pressed("move_up".into()) {
vel.y -= 1.0;
}
if input_obj.is_action_pressed("move_down".into()) {
vel.y += 1.0;
}
// if vel != Vector2::ZERO {
// godot_error!("fuck you")
// }
let pos = self.base().get_position();
self.base_mut()
.set_position(pos + vel.normalized() * Self::SPEED as f32 * delta as f32);
// godot_print!("position move")
}
}

#[godot_api]
impl Player {
#[constant]
const SPEED: i32 = 500;
}
8 changes: 4 additions & 4 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ window/size/resizable=false
move_right={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
move_left={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
move_up={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
move_down={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
Binary file added resources/images/PlayerHeart/Default/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions resources/images/PlayerHeart/Default/000.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://c71fsdux0i1r3"
path="res://.godot/imported/000.png-a4b21812678d9682ba51d4cdb5a01175.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://resources/images/PlayerHeart/Default/000.png"
dest_files=["res://.godot/imported/000.png-a4b21812678d9682ba51d4cdb5a01175.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
Binary file added resources/images/PlayerHeart/Split/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions resources/images/PlayerHeart/Split/000.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://b7yfjvvu14mf"
path="res://.godot/imported/000.png-48f47f0c96b2a076c5508fb2baa6aa87.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://resources/images/PlayerHeart/Split/000.png"
dest_files=["res://.godot/imported/000.png-48f47f0c96b2a076c5508fb2baa6aa87.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
19 changes: 16 additions & 3 deletions scenes/fight.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[gd_scene load_steps=5 format=3 uid="uid://cibwxaqnuodid"]
[gd_scene load_steps=7 format=3 uid="uid://cibwxaqnuodid"]

[ext_resource type="Script" path="res://scripts/fight.gd" id="1_fov6f"]
[ext_resource type="Texture2D" uid="uid://cqp3l8q7xbxrt" path="res://resources/images/weapons/Zenith.webp" id="3_2spkb"]
[ext_resource type="Script" path="res://scripts/Player.gd" id="3_6nc6b"]
[ext_resource type="PackedScene" uid="uid://dn2ixin15jtt3" path="res://scenes/weapons/star_wrath.tscn" id="4_8ipx5"]
[ext_resource type="Texture2D" uid="uid://c71fsdux0i1r3" path="res://resources/images/PlayerHeart/Default/000.png" id="5_3ufqu"]

[sub_resource type="CircleShape2D" id="CircleShape2D_yfrnq"]

[node name="Fight" type="Control"]
layout_mode = 3
Expand All @@ -29,9 +32,19 @@ rotation = 2.35389
scale = Vector2(-3.28571, -3.28571)
texture = ExtResource("3_2spkb")

[node name="Star_Wrath" parent="." instance=ExtResource("4_8ipx5")]

[node name="Player" type="Player" parent="."]
position = Vector2(570, 463)
rotation = 1.5708
scale = Vector2(0.2, 0.2)
script = ExtResource("3_6nc6b")

[node name="CollisionShape2D" type="CollisionShape2D" parent="Player"]
[node name="HitPoint" type="CollisionShape2D" parent="Player"]
shape = SubResource("CircleShape2D_yfrnq")

[node name="Star_Wrath" parent="." instance=ExtResource("4_8ipx5")]
[node name="Sprite2D" type="Sprite2D" parent="Player"]
scale = Vector2(6.6, 6.6)
texture = ExtResource("5_3ufqu")

[node name="BlockDrawer" type="BlockDrawer" parent="."]

0 comments on commit 55adc99

Please sign in to comment.