Skip to content

Commit

Permalink
upd tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Lomaev committed May 31, 2024
1 parent f90be71 commit 08284a7
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import AdaEngine

@Component
struct PlayerComponent { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import AdaEngine

func makePlayer(for scene: Scene) throws {
}
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)
}

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)
}

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)
}
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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import AdaEngine

@Component
struct GameState {
var score: Int = 0
}
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)
}
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)
}

}
}
}

0 comments on commit 08284a7

Please sign in to comment.