lecture 2 godot jumping and gravitystaff lecture 2.pdf24. add a child node to the background node...

15
Lecture 2 – Godot – Jumping and Gravity In this lecture you will learn the following: How to create a player object that uses collision How to use gravity How to create platforms and collision areas How to jump your character How to have your character shoot objects 1. Download the lecture 2 zip file. This will contain some sprite sheets and other objects. 2. Start a new Godot project named Lecture2 and save it in your godot projects folder. 3. Create a new node 2D and name it scene1. 4. Right click the res:\\ folder and create the following folders. backgrounds sprites scripts 5. Drag the wallpaper bg_4_02 and bg_2_01 to the backgrounds folder. 6. Drag the sprite sheets to the sprites folder and drag the platforms and pumpkins files into the sprites folder. 7. Create a Kinesmaticbody2D node below the scene1 node. Rename this node Player. 8. Add the following child nodes to the Player node and rename them as indicated. Player AnimatedSprite – PlayerSprite CollisionShape2D – Playercoll 9. Select the player node and click the button to make the child nodes not selectable. 10. Add a child node to Scene1 that is of type Sprite and rename it Background 11. Use the project setting menu to change the Display size to 1024 by 720 12. Click the background node and drag the image bg_4_02 and drop it on the texture box. 13. Open the offset option and uncheck Centered. 14. Click the playersprite node. In the inspector window click the Frames option and choose SpriteFrames. 15. In the animation window click the Add Frames from sprite sheet. 16. Choose Dinoleft for the sprite sheet.

Upload: others

Post on 02-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

Lecture 2 – Godot – Jumping and Gravity

In this lecture you will learn the following:

• How to create a player object that uses collision

• How to use gravity

• How to create platforms and collision areas

• How to jump your character

• How to have your character shoot objects

1. Download the lecture 2 zip file. This will contain some sprite sheets and other objects.

2. Start a new Godot project named Lecture2 and save it in your godot projects folder.

3. Create a new node 2D and name it scene1.

4. Right click the res:\\ folder and create the following folders.

backgrounds

sprites

scripts

5. Drag the wallpaper bg_4_02 and bg_2_01 to the backgrounds folder.

6. Drag the sprite sheets to the sprites folder and drag the platforms and pumpkins files into the sprites folder.

7. Create a Kinesmaticbody2D node below the scene1 node. Rename this node Player.

8. Add the following child nodes to the Player node and rename them as indicated.

Player

→ AnimatedSprite – PlayerSprite

→ CollisionShape2D – Playercoll

9. Select the player node and click the button to make the child nodes not selectable.

10. Add a child node to Scene1 that is of type Sprite and rename it Background

11. Use the project setting menu to change the Display size to 1024 by 720

12. Click the background node and drag the image bg_4_02 and drop it on the texture box.

13. Open the offset option and uncheck Centered.

14. Click the playersprite node. In the inspector window click the Frames option and choose SpriteFrames.

15. In the animation window click the Add Frames from sprite sheet.

16. Choose Dinoleft for the sprite sheet.

Page 2: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

17. Click the horizontal 5 and rows 3

18. Click each dino image and click Add 12 frames. (If the background is on top of the player, drag the background to

just below the Scene1 node.)

19. Drag the dino onto the background. You will next set the collision area for the image.

20. Click the playercoll node (which is the collision node). In the inspector window you see the Shape option.

You will need to define the shape of the collision zone.

Page 3: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

21. Click the dropdown and choose New RectangleShape2D. A small set of dots appear on the object. Drag these to

the right an up until the object is covered.

22. To turn off the collision display box click the playercoll node and in the Inspector window under visibility

uncheck visible.

23. Run your scene. The dino does not fall because we need to implement gravity in a script. However, before we

add gravity we should create a collision zone for the floor of the background.

In order to draw a collision zone you must make it a child of a Rigidbody or Kinesmatic node. You can attach this

to the background.

24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a

child to the Kinesmaticbody2D node.

25. For the collisionshape2D node set it to the new segmentshape2D. When you run your game the background

collision zone will stop the fall.

26. Choose the segmentshape node and locate the object select handles. Draw a line along the bottom of the screen

next to the floor.

Page 4: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

27. Run the scene and notice that the player falls but stops when they hit the ground.

You will next add a script to the Dino.

Movement Script

1. Right click the player node and create a script and put the script in the scripts folder.

Page 5: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

2. Enter the following text in the script.

Page 6: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D
Page 7: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

Looking at this script there are various interesting elements:

In these lines of code the term export (int) var … puts these variables into the Godot GUI properties list under Inspector

for this node.

This allows the player to set the values of the character from the game engine gui and not only from code.

Following these export variables is a regular variable which will be used by methods in the script.

var velocity = Vector2.ZERO

This script has three functions:

func centerCamera()

func get_input()

func _physics_process(delta)

The last method is automatically called during the game loop. When the game runs the game engine looks for built-in

methods such as _physics_process(delta) and calls these methods. The value delta is passed into this process and

represents the amount of time that has elapsed since the last call (the amount of time it took the game loop to

complete). The delta for physics process is always the same value and is synched to the physics process.

Other important loop methods that are automatically called are:

func _ready() – this is called once for each node when it has been initialized. If a node has child nodes then the ready()

function for each child node is called before the parent node.

Page 8: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

func _process(delta) – this is called each time through the game loop and the delta value will not always be the same

value. If a game loop takes longer then the delta will be larger.

In this particular script the programmer has created the centerCamera() and get_input() methods. Look at the

_physics_process(delta) method.

The get_input() function is called. This function retrieves information about the keyboard input. The velocity variable

(declared above this function and is a Vector2 data type) has the y value increased by multiplying the gravity value by

the delta. Since the delta is a fractional value (the number of seconds it takes to get through a game loop) and gravity is

set to 200, then each time through the game loop the gravity value is increased.

Below this increase to gravity is an if statement that checks the value of the velocity.y value and sets it to 600 if it is over

600. This sets gravity with a terminal velocity.

The function on line 35 moves the player node in the direction contained in the velocity vector. The y value (up and

down) is set by the gravity value and the jump value while the x value is set in the get_input() function.

Notice that the second parameter to the move_and_slide() function is Vector2(0, -1). This second parameter is used to

tell the game engine which direction is down so the built-in method is_on_floor() can be used. If the game engine knows

which direction the floor is located then it can check to see if the player is on the floor. The Vector2(0, -1) indicates that

the negative value for y means that up is the negative value for y.

Where is the code checking the keyboard input? In this done in the get_input() function. Notice that each time through

the game loop the _physics_process(delta) function is called. The get_input() method is called as the first statement of

this loop function. That is, the first thing that happens is that the input keys are checked.

The get_input() method looks like:

Page 9: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

There are two purposes of the get_input() function. One of these is to check the keyboard and the second purpose is to

set the velocity vector and flip the direction of the sprite. The velocity vector has two values, X and Y. This velocity vector

is defined as a var at the top of the script. It can be accessed throughout all of the functions in the script.

This function uses the Input object to check for three keyboard inputs. The “ui_right”, “ui_left”, and “ui_select” keys.

The ui_right and ui_left key checks set the x value of the velocity vector to speed or -speed. The function also locates the

playersprite node and flips the direction of the animated sprite. If neither left or right keys are pressed the x value of the

velocity vector is set to zero.

After the x value for velocity is set the “ui_select” (spacebar) key is checked and, if the player is on the floor, then the y

value of the velocity variable has been set. The player will jump into the air and then be pulled down by gravity (which is

being applied in the _physics_process() function.

The final method is used to center the camera so the player is focused.

The position of the player is saved with position.x and position.y and this is then applied to the camera node.

Page 10: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

Throwing Pumpkins

So far, we have been using nodes that have been created under the scene node. The scene root node appears in the

screen when the game runs. To create a node object dynamically you will need to create and save a new Scene that

represents the object to be thrown. This is then loaded by your main game scene.

1. Right click the res:\\ link, choose New Folder, and create a new folder named Bullets.

2. Click the Scene menu and choose New Scene. This creates another tab to the main menu.

3. Click on the new empty tab and click the + Other Node option and choose an AnimatedSprite node. Rename this

to pumpkin.

4. Add a child node to pumpkin named VisibilityNotifier2D. This will be used to remove the node if it goes off the

screen.

5. Select the pumpkin node and click the New Sprite Frames. The frames menu opens at the bottom of the screen.

6. Click the add frames from a sprite sheet button and choose the pumpkin sprite sheet.

7. Set the horizontal to six and the vertical to two and then select all of the pumpkin images.

8. Select your pumpkin node and click the playing check box to see the animation.

9. Right click the pumpkin sprite and choose Add Script. Name the script pumpkin and save it in the scripts folder.

Page 11: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

10. Write the following script for the pumpkin

Notice that since this is not a physics object the global position must be directly set by using velocity and delta.

Now that we have a bullet we can attach it to the main scene and have the player throw a pumpkin.

Page 12: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

11. Go back to the main scene and edit the script to look like: extends KinematicBody2D

export (int) var speed = 220

export (int) var jump_speed = -300

export (int) var gravity = 200

var velocity = Vector2.ZERO

var pausecount = 0

var facedir = "left"

const BULLET = preload("res://bullets/bullet1.tscn")

const PAUSEFIRE = 1

func centerCamera():

var w = position.x

var h = position.y

var n = find_node("Camera2D")

n.offset.x = w

n.offset.y = h

n.drag_margin_h_enabled = true

n.drag_margin_v_enabled = true

func shoot():

if( pausecount > PAUSEFIRE):

var b = BULLET.instance()

if( facedir=="left"):

b.velocity.x = -200

if( facedir=="right"):

b.velocity.x = 200

b.global_position = global_position

get_parent().add_child(b)

pausecount = 0

func get_input():

if Input.is_action_pressed("ui_right"):

velocity.x = speed

find_node(("playersprite")).flip_h = true

facedir = "right"

elif Input.is_action_pressed("ui_left"):

velocity.x = -speed

find_node(("playersprite")).flip_h = false

facedir = "left"

else:

velocity.x = 0

if Input.is_action_just_pressed("ui_select"):

if is_on_floor():

velocity.y = jump_speed

if Input.is_key_pressed(KEY_F):

shoot()

Page 13: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

func _physics_process(delta):

get_input()

pausecount += delta

velocity.y += gravity * delta

if(velocity.y > 600):

velocity.y = 600

velocity = move_and_slide(velocity, Vector2(0,-1))

Looking at this script we see the following:

A new variable facedir = “left” was added along with a pausecount variable that will be used to count time. Also notice

the const values. BULLET contains a scene that contains the pumpkin. Notice how it was loaded using the preload()

method and a string indicating the scene name. Also, the value PAUSEFIRE is how long you must wait before firing.

A new method named shoot() was added.

When this function is called it first checks to see if the pausecount is greater than the PAUSEFIRE (which is 1 second). If

the player can shoot an instance of the BULLET scene is created with the instance() method. This object velocity is set

based on whether the player is facing left or right. The speed is 200.

Once the object speed is set the position of the bullet is set to the position of the player (global_position). Finally, the

instance is added to the parent node by u sing the get_parent().add_child(b) command.

Page 14: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D

The input function is changed to add a check for the letter F being pressed and calling the shoot function.

Finally, the delta value in the _physics_process() method is used to update the pause count.

Page 15: Lecture 2 Godot Jumping and Gravitystaff lecture 2.pdf24. Add a child node to the Background node and make it a Kinesmaticbody2D node. Add a collisionshape2D as a child to the Kinesmaticbody2D