Skip to content

Commit

Permalink
new_node to main (Chukobyte#7)
Browse files Browse the repository at this point in the history
* Added ability to create entities through python scripting (instead of just defining in scenes).
* Update logic for parent child relationship within a scene.
* Added CollisionShape2D node type for collision handling.
* Added Rect2 in math module.
* Updated docs
  • Loading branch information
Chukobyte authored May 17, 2021
1 parent f7275cc commit 0c26d5d
Show file tree
Hide file tree
Showing 21 changed files with 630 additions and 55 deletions.
89 changes: 87 additions & 2 deletions assets/game_projects/fighter/scenes/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,50 @@
}
}
],
"children": []
"children": [
{
"name": "PlayerOneHitBox",
"type": "CollisionShape2D",
"tags": [],
"external_scene_source": "",
"components": [
{
"transform2D": {
"position": {
"x": 0,
"y": 0
},
"scale": {
"x": 4,
"y": 4
},
"rotation": 0,
"z_index": 0,
"z_index_relative_to_parent": false,
"ignore_camera": false
}
},
{
"collider": {
"rectangle": {
"x": 0.0,
"y": 0.0,
"width": 4.0,
"height": 4.0
}
}
},
{
"scriptable_class": {
"class_path": "assets.game_projects.fighter.src.hit_box",
"class_name": "HitBox"
}
}
],
"children": []
}

]
},
{
"name": "PlayerTwo",
Expand Down Expand Up @@ -420,7 +463,49 @@
}
}
],
"children": []
"children": [
{
"name": "PlayerTwoHitBox",
"type": "CollisionShape2D",
"tags": [],
"external_scene_source": "",
"components": [
{
"transform2D": {
"position": {
"x": 0,
"y": 0
},
"scale": {
"x": 4,
"y": 4
},
"rotation": 0,
"z_index": 0,
"z_index_relative_to_parent": false,
"ignore_camera": false
}
},
{
"collider": {
"rectangle": {
"x": 0.0,
"y": 0.0,
"width": 4.0,
"height": 4.0
}
}
},
{
"scriptable_class": {
"class_path": "assets.game_projects.fighter.src.hit_box",
"class_name": "HitBox"
}
}
],
"children": []
}
]
}
]
}
17 changes: 17 additions & 0 deletions assets/game_projects/fighter/src/hit_box.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import roll_engine_api

from roll.node import CollisionShape2D


class HitBox(CollisionShape2D):
# TODO: Investigate way to make child not have to implement
@classmethod
def new(cls):
return roll_engine_api.node_new(
class_path=f"{__name__}",
class_name=f"{cls.__name__}",
node_type=f"{cls.extract_valid_inheritance_node()}",
)

def _start(self) -> None:
pass
4 changes: 2 additions & 2 deletions assets/game_projects/fighter/src/init.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from roll.node import Node
from roll.node import Node2D
from roll.scene import SceneTree


class Init(Node):
class Init(Node2D):
def _start(self) -> None:
SceneTree.change_scene(
scene_path="assets/game_projects/fighter/scenes/title_screen.json"
Expand Down
14 changes: 11 additions & 3 deletions assets/game_projects/fighter/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,19 @@ def _physics_process(self, delta_time: float) -> None:
def _process_inputs(self) -> None:
if Input.is_action_just_pressed(action_name="quit"):
# Go back to main menu
if self.game_properties.player_opponent_mode == PropertyValue.PLAYER_OPPONENT_MODE_HOST_PLAYER_VS_PLAYER:
if (
self.game_properties.player_opponent_mode
== PropertyValue.PLAYER_OPPONENT_MODE_HOST_PLAYER_VS_PLAYER
):
Server.stop()
elif self.game_properties.player_opponent_mode == PropertyValue.PLAYER_OPPONENT_MODE_CLIENT_PLAYER_VS_PLAYER:
elif (
self.game_properties.player_opponent_mode
== PropertyValue.PLAYER_OPPONENT_MODE_CLIENT_PLAYER_VS_PLAYER
):
Client.disconnect()
SceneTree.change_scene(scene_path="assets/game_projects/fighter/scenes/title_screen.json")
SceneTree.change_scene(
scene_path="assets/game_projects/fighter/scenes/title_screen.json"
)
# Engine.exit()

for input_buffer in self.input_buffers:
Expand Down
21 changes: 21 additions & 0 deletions assets/game_projects/fighter/src/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import roll_engine_api
from roll.math import Rect2
from roll.node import CollisionShape2D


class Test(CollisionShape2D):
@classmethod
def new(cls):
return roll_engine_api.node_new(
class_path=f"{__name__}",
class_name=f"{__class__.__name__}",
node_type=f"{cls.extract_valid_inheritance_node()}",
)

def _start(self) -> None:
self.collider_rect = Rect2(x=300, y=300, w=40, h=40)
print(f"Start called! Rect = {self.collider_rect}")

def _physics_process(self, delta_time: float) -> None:
# print("Process!")
pass
5 changes: 5 additions & 0 deletions assets/game_projects/fighter/src/title_screen.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
from enum import auto

import roll_engine_api

from roll.node import Node2D
from roll.scene import SceneTree
from roll.input import Input
from roll.engine import Engine
from roll.color import Color
from roll.math import Vector2


from assets.game_projects.fighter.src.auto_enum import AutoName

from assets.game_projects.fighter.src.game_properties import (
GameProperties,
PropertyValue,
)
from assets.game_projects.fighter.src.test import Test


class MenuSelection(AutoName):
Expand Down
72 changes: 72 additions & 0 deletions docs/general/project_properties.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Project Properties

Project properties define how the game will be configured before running. An example of the file is found below:

### Format

```json
{
"game_title": "Fighting Game Proto",
"initial_scene": "scenes/init.json",
"base_resolution": {
"width": 800,
"height": 600
},
"colliders_visible": true,
"assets": [
{
"type": "texture",
"file_path": "assets/game_projects/fighter/assets/fighters/puncher/puncher_basic_sheet.png"
},
{
"type": "font",
"file_path": "assets/fonts/bruh.ttf",
"size": 60
},
{
"type": "music",
"file_path": "assets/audio/music/test_music.wav"
},
{
"type": "sound",
"file_path": "assets/audio/sound/test_sound_effect.wav"
}
],
"input_actions": [
{
"name": "quit",
"values": ["esc"]
},
{
"name": "confirm",
"values": ["return"]
}
]
}
```

### Properties

`game_title`

Title of the game window.

`initial_scene`

First scene loaded for the game.

`base_resolution`

Base resolution of the game

`colliders_visible`

If true, will render a visible box for colliders.

`assets`

Textures, fonts, music, and sound effect assets are defined here.

`input_actions`

Key bindings are defined here.
42 changes: 42 additions & 0 deletions docs/python_api/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Add to node's current position. For example, if this line of code is within the

**Inherits**: [Node2D](#node2d) -> [Node](#node)

Class used to render a sprite entity.

### Variables

```python
Expand Down Expand Up @@ -167,6 +169,8 @@ None.

**Inherits**: [Node2D](#node2d) -> [Node](#node)

Class used to render an animated sprite entity.

### Variables

```python
Expand Down Expand Up @@ -225,6 +229,8 @@ Stops currently playing animation.

**Inherits**: [Node2D](#node2d) -> [Node](#node)

Class used to render font.

### Variables

```python
Expand Down Expand Up @@ -262,3 +268,39 @@ set_text(value: str) -> None:
Set node's label text.

---

---

## CollisionShape2D

**Inherits**: [Node2D](#node2d) -> [Node](#node)

Class used to define collision shapes defined as rectangles. May add other collision shapes in the future.

### Variables

```python
collider_rect: roll.math.Rect2
```

Collision shape's colliding rectangle.

```python
nodes_to_exclude: roll.color.Node
```

Nodes that should be excluded from collision checks.

---

### Signals

None.

---

### Methods

None.

---
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ nav:
- General:
- Home: index.md
- Core Concepts: general/core_concepts.md
- Project Properties: general/project_properties.md
- Python API:
- Index: python_api/index.md
- Node: python_api/node.md
Expand Down
2 changes: 1 addition & 1 deletion src/core/ecs/component/components/collider_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

struct ColliderComponent {
public:
Rect2 collider;
Rect2 collider = Rect2();
};

#endif //COLLIDER_COMPONENT_H
22 changes: 17 additions & 5 deletions src/core/ecs/component/components/node_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@
#include "../component.h"

using NodeType = std::uint32_t;
using NodeTypeInheritance = std::uint32_t;

enum _NodeType {
NodeType_INVALID = 0,
NodeType_NODE = 1,
NodeType_NODE2D = 2,
NodeType_SPRITE = 3,
NodeType_ANIMATED_SPRITE = 4,
NodeType_TEXT_LABEL = 5,
NodeType_NODE = 2,
NodeType_NODE2D = 4,
NodeType_SPRITE = 8,
NodeType_ANIMATED_SPRITE = 16,
NodeType_TEXT_LABEL = 32,
NodeType_COLLISION_SHAPE2D = 64,
};

enum _NodeTypeInheritance {
NodeTypeInheritance_INVALID = NodeType_INVALID,
NodeTypeInheritance_NODE = NodeType_NODE,
NodeTypeInheritance_NODE2D = NodeType_NODE | NodeType_NODE2D,
NodeTypeInheritance_SPRITE = NodeType_NODE | NodeType_NODE2D | NodeType_SPRITE,
NodeTypeInheritance_ANIMATED_SPRITE = NodeType_NODE | NodeType_NODE2D | NodeType_ANIMATED_SPRITE,
NodeTypeInheritance_TEXT_LABEL = NodeType_NODE | NodeType_NODE2D | NodeType_TEXT_LABEL,
NodeTypeInheritance_COLLISION_SHAPE2D = NodeType_NODE | NodeType_NODE2D | NodeType_COLLISION_SHAPE2D,
};

struct NodeComponent {
Expand Down
Loading

0 comments on commit 0c26d5d

Please sign in to comment.