-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Danil Lomaev
committed
May 31, 2024
1 parent
f90be71
commit 08284a7
Showing
9 changed files
with
129 additions
and
10 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
Sources/AdaEngine/AdaEngine.docc/Resources/PlayerAndText/Code/PAT-1-1-player-component.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import AdaEngine | ||
|
||
@Component | ||
struct PlayerComponent { } |
4 changes: 4 additions & 0 deletions
4
Sources/AdaEngine/AdaEngine.docc/Resources/PlayerAndText/Code/PAT-1-2-makePlayer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import AdaEngine | ||
|
||
func makePlayer(for scene: Scene) throws { | ||
} |
10 changes: 10 additions & 0 deletions
10
Sources/AdaEngine/AdaEngine.docc/Resources/PlayerAndText/Code/PAT-1-3-makePlayer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import AdaEngine | ||
|
||
func makePlayer(for scene: Scene) throws { | ||
let player = Entity() | ||
|
||
player.components += PlayerComponent() | ||
|
||
scene.addEntity(player) | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
Sources/AdaEngine/AdaEngine.docc/Resources/PlayerAndText/Code/PAT-1-4-makePlayer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import AdaEngine | ||
|
||
func makePlayer(for scene: Scene) throws { | ||
let player = Entity() | ||
|
||
player.components += Transform(scale: Vector3(0.2), position: [0, -0.85, 0]) | ||
player.components += PlayerComponent() | ||
|
||
scene.addEntity(player) | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
Sources/AdaEngine/AdaEngine.docc/Resources/PlayerAndText/Code/PAT-1-5-makePlayer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import AdaEngine | ||
|
||
func makePlayer(for scene: Scene) throws { | ||
let player = Entity() | ||
|
||
player.components += Transform(scale: Vector3(0.2), position: [0, -0.85, 0]) | ||
player.components += PlayerComponent() | ||
player.components += SpriteComponent(texture: characterAtlas[7, 1]) | ||
|
||
scene.addEntity(player) | ||
} |
7 changes: 7 additions & 0 deletions
7
Sources/AdaEngine/AdaEngine.docc/Resources/PlayerAndText/Code/PAT-1-6-makeScene.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import AdaEngine | ||
|
||
@MainActor | ||
func makeScene() async throws -> Scene { | ||
let scene = Scene() | ||
try self.makePlayer(for: scene) | ||
} |
6 changes: 6 additions & 0 deletions
6
Sources/AdaEngine/AdaEngine.docc/Resources/PlayerAndText/Code/PAT-2-1-gameState.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import AdaEngine | ||
|
||
@Component | ||
struct GameState { | ||
var score: Int = 0 | ||
} |
15 changes: 15 additions & 0 deletions
15
Sources/AdaEngine/AdaEngine.docc/Resources/PlayerAndText/Code/PAT-2-2-makeScore.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import AdaEngine | ||
|
||
func makeScore(for scene: Scene) throws { | ||
let score = Entity(name: "Score") | ||
|
||
var container = TextAttributeContainer() | ||
container.foregroundColor = .white | ||
let attributedText = AttributedText("Score: 0", attributes: container) | ||
|
||
score.components += Text2DComponent(text: attributedText) | ||
score.components += GameState() | ||
score.components += Transform(scale: Vector3(0.1), position: [-0.2, -0.9, 0]) | ||
|
||
scene.addEntity(score) | ||
} |
71 changes: 61 additions & 10 deletions
71
Sources/AdaEngine/AdaEngine.docc/Tutorials/AdaEngine/tutorials/PlayerAndText.tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,80 @@ | ||
@Tutorial(time: 5) { | ||
@Intro(title: "<#text#>") { | ||
<#text#> | ||
@Intro(title: "Player and Text in Ada Engine") { | ||
|
||
@Image(source: <#file#>, alt: "<#accessible description#>") | ||
In this tutorial, you will learn how to add a player component that you can control and how to add text and various labels to your game scene. | ||
} | ||
|
||
@Section(title: "<#text#>") { | ||
@Section(title: "Add Player component") { | ||
@ContentAndMedia { | ||
<#text#> | ||
|
||
@Image(source: <#file#>, alt: "<#accessible description#>") | ||
Here we will start adding our Components and Entity, so that AdaEngine will correctly render our player and make him alive! | ||
|
||
} | ||
|
||
@Steps { | ||
@Step { | ||
<#text#> | ||
First of all you have to add the player component. For AdaEngine to understand you, just specify the following code. | ||
|
||
@Code(name: "PlayerComponent.swift", file: PAT-1-1-player-component.swift) | ||
} | ||
|
||
@Step { | ||
We must add our player to the scene. To do this we will use Entity and a couple of helper functions. Follow the code! | ||
|
||
@Image(source: <#file#>, alt: "<#accessible description#>") | ||
We will create a separate makePlayer function for our scene where we will add various components. | ||
|
||
@Code(name: "MakePlayer.swift", file: PAT-1-2-makePlayer.swift) | ||
} | ||
|
||
@Step { | ||
<#text#> | ||
|
||
@Code(name: "<#display name#>", file: <#filename.swift#>) | ||
Next, let's declare that our player is an Entity, which can contain several components for various modifications and features in the future. First, let's add the PlayerComponent, which we discussed earlier, so that we can then easily indicate that this entity is our player. At the end, don't forget to add the player entity to the scene. | ||
|
||
@Code(name: "MakePlayer.swift", file: PAT-1-3-makePlayer.swift) | ||
} | ||
|
||
@Step { | ||
AdaEngine knows that the player has been added to the scene, but... he can't do anything and doesn't have any properties. Let's fix this! To begin with, we will give the player the opportunity to move and, in general, somehow change his size and position. Let's use the built-in Transform component and set some initial values | ||
|
||
@Code(name: "MakePlayer.swift", file: PAT-1-4-makePlayer.swift) | ||
} | ||
|
||
@Step { | ||
|
||
One more component is missing - what will the player look like? To do this, you can use sprites and the corresponding component in AdaEngine | ||
|
||
@Code(name: "MakePlayer.swift", file: PAT-1-5-makePlayer.swift) | ||
} | ||
|
||
@Step { | ||
|
||
After all the preparations, let's call our function in makeScene | ||
|
||
@Code(name: "MakePlayer.swift", file: PAT-1-6-makeScene.swift) | ||
} | ||
} | ||
} | ||
|
||
@Section(title: "Add Text component") { | ||
@ContentAndMedia { | ||
Let's dive deeper into the AdaEngine text component | ||
} | ||
|
||
@Steps { | ||
@Step { | ||
Okay, most likely you want to count some points for your player. Let's start creating the GameState component | ||
|
||
@Code(name: "GameState.swift", file: PAT-2-1-gameState.swift) | ||
} | ||
|
||
@Step { | ||
By analogy with the player, we will create a separate function for adding glasses text to the scene. Here, we also create an Entity, add a Text2DComponent to it - it will help us draw the text. We add our GameState to take updated values from the state and add Transform. | ||
|
||
To customize the text, you can use TextAttributeContainer. | ||
|
||
@Code(name: "MakeScore.swift", file: PAT-2-2-makeScore.swift) | ||
} | ||
|
||
} | ||
} | ||
} |