Sunday, January 12, 2014

Blender Game Engine Key Door

In almost every game we see some kind of door or blocked which player needs to open with key or another way. We are going to make a simple key door which will open when player will press 'E' key.

Start with a new project. Delete everything and save the project as 'KeyDoor.blend'.

Now create a plane mesh - go to - 'Add->Mesh->Plane'.
Scale the plane a little bit so that we have some grounds to move the player.
Now add a cube to make the door - go to 'Add->Mesh->Cube'.
Scale and move the cube as you like to make a door.
I am going to use a rotating door. So, we have to change the pivot point of the door to make it rotate like a door. Go to edit mode and press 'Ctrl+A' to select all and move the door on Y axis.
Press 'Tab' to exit edit mode.

You can add some more cubes and make it walls at both side of the door, if you want.
I am going to use action, rotate animation, to open and close the door.

Select the door - go to 'Timeline Editor' - add a key on frame 1 when it is closed. To add a key press 'I' and select 'Rotation' from the pop up menu.

Move the playhead to frame 30, now rotate the door as you like and insert a key.
Now move the playhead to frame 60 and rotate the door back to its close position and insert a key frame. You can use 'Dopesheet Editor' to copy and paste the frame 1 to frame 60, because both are same position.
Door still selected, open the 'Logic Editor'. Add two 'Message' sensors, add two 'And' controller and add two 'Action' actuators.
Insert 'open' in the first message sensor's subject, insert 'close' in second message sensor's subject. In action actuators, select the 'CubeAction' in value input for both the actuators. In first action set the start frame to 1 and end frame to 30. In second actuators set the start frame to 30 and end frame to 60.
Note: You can change the action name in the 'Action Editor'.
 We are done with door. It is ready for the action. Save your project.

I am not going to make a complete player object. For this example I am going to make a simple player who only moves forward and backward. Make the player as you like.
Open the 'Logic Editor', select your player and add a property - 'player'. Type does not matter here.


Add some movement logic to the player as you like.

Add a cube, scale - position it in front of the door. From now on I will call it door guard :).


Go to 'Physics' panel and change the physics type to 'Sensor'.
Set the physics visibility to Invisible. Click the 'Invisible' button.
That door guard still selected, open 'Logic Editor'. Add a property, name it 'open', change its type to boolean.
Add a 'Collision' sensor. Add a Keyboard sensor. Add a 'Python' controller and add two 'Message' actuator. Name them as you like or leave as it is.

In the collision sensor, add 'player' in the 'Property' input. Now it will only check for the objects which has 'player' property in it. In the keyboard insert 'E' in 'Key' input and click the 'Tap' button. In the controller select the python script( I name it 'KeyDoor.py', see below for the script ). With the actuators, select the door in the 'To' input from the drop down menu for both the actuators.
Add 'open' in first actuator and add 'close' in second actuator in the 'Subject' input field.

Now open the 'Text Editor' and create a new text. Name it 'KeyDoor.py( as mentioned above ).
Here is the python code for the KeyDoor.py

import bge
cont = bge.logic.getCurrentController()
own = cont.owner

#Sensors
coll = cont.sensors["Collision"]
key = cont.sensors["Keyboard"]

#Actuators
open = cont.actuators["Message"]
close = cont.actuators["Message1"]

if coll.positive:
    #do something here,
    #like pop a message - "Press E to open the door"
    if key.positive:
        own["open"] = not own["open"]
       
        if own["open"]:
            cont.activate(open)
        else:
            cont.activate(close)



Press 'P' and enjoy.

Here is the finish .blend file - KeyDoor.blend

Occult : Chapter One

I am making a FPS Horror game inspired by SCP:CB. Without writing the GDD first, I just started to design the map. Then another fan of SCP:CB came forward to help me with the game story. I agreed (obviously I would ) :) and when the story is written and GDD is designed we name the new game as SCP:Nightmares. Then I found out that the current map, which I was designing for the game, does not suit the story. I had to exclude the map and re design the whole map from scratch.

Rather than left the current map untouched and let it rot in the hard drive I decided to make a new game with it. So a new title with a new design came out, which is in WIP right now, called - 'Occult : Chapter One'.

Some screenshot from the game.


SCP:Nightmares is still in WIP and once I finish that I will start working on it again. It will be a very small FPS Horror game.

Blender Game Engine Menu

Whenever I download a game engine, first of all I always ask 'How to create a menu'. So, lets create a menu using my favorite game engine 'Blender Game Engine'.

Start a new project. Remove everything from the viewport - press 'Ctrl+A' to select everything and then press 'Delete'. Now we have an empty project. Save the project with a name something like - Menu.blend.

First we will need a camera - go to menu - 'Add->Camera'.

A camera will be created facing down side. Move that upwards a bit on z axis.
Don't forget to save your project.

Now create a plan - go to menu - 'Add->Mesh->Plane'. A plane object will be created.

Here is the button images that we will use to make the menu.

I have made very simple two buttons, each with normal and hover images. The blue outlined button will be used as normal button and white outlined button will be used as hover button.

Texture the button using above image. Make the material 'Shadeless' -

and create four buttons with it.
Now go to camera view - press '0' on numpad. Move the two buttons with white outline out side the camera view. Scale - position the the two images as you like.
We have now our basic setup for the menu. Lets make them into working menu buttons.

Before we add scripts, rename the buttons with a unique name each.
Now select the two "Normal' buttons and duplicate it. Move that below the 'Hover' buttons. And rename the two buttons that we have inside camera view.
We will use 'Replace Mesh' to replace the mesh of the object during runtime. So, just changing the name of the object won't work here. We will need to rename the object data as well. So, do it now with each buttons.
Note: There are other ways to create the buttons. Like changing the visibility of the object, changing the position of the object, using the action etc. I am going with Replace mesh. It is up to you what you choose.

Open the 'Logic Editor' panel. Select the play button. You can make the menu:
One - only using the logic brick
Two - with python only and
Three - using both.
I am going to use both logic brick and python. You can try the other way yourself, if you want.
 
Play button still selected - add two 'Mouse' sensor - add a 'Python' controller and add two 'Edit Object' actuators
Name the actuators and sensors to something that suits our needs. We will need a mouse click and mouse over actuator. So, change the second actuator to 'Mouse Over'. In controller, select the python script( I am using Menu.py, see below for the script ) from the drop down list. With sensors, change the 'Add Object' to 'Replace Mesh' and select the appropriate mesh name from the drop down list.


Do it with 'Quit' button also. Don't forget to save your project.

Now open 'Text Editor' panel and create a new text. Name it to 'Menu.py'( as mentioned above or change it to whatever you like).
Here is the python code that we will use for both 'Play' and 'Quit' button.

import bge
cont = bge.logic.getCurrentController()
own = cont.owner

#sensors
lClick = cont.sensors["lClick"]
mouse_over = cont.sensors["MouseOver"]

#actuators
normal = cont.actuators["Normal"]
hover = cont.actuators["Hover"]

if mouse_over.positive:
    #change the mesh to hover
    cont.activate(hover)
   
    if lClick.positive:
        #do whatever you want
        print("You clicked me")
else:
    cont.activate(normal)

By default mouse cursor is set to hidden. We need to unhide the mouse cursor before we run the game. Go to 'Render' panel and check the 'Mouse Cursor' check box.
Now press 'P' and enjoy :).

Here is the finish .blend file Menu.blend