Heavily Commented it to show how the code works.
----CUSTOMIZABLE GAME SCRIPT----
----OBJECTS
--This is where you create all of the objects in your game
box=getObject("cube")
man=getObject("man")
monkey = getObject("monkey")
cone = getObject("cone")
person = getObject("person")
steps = getObject("steps")
Player = getObject("Player")
Set = getObject("Set")
Feet = getObject("Feet")
apple = getObject("apple")
snd_collect = getObject("collect")
apple_clone = getClone(apple)
----END OBJECTS
----MOTION (Self-Explanatory)
--MOVE VARIABLES
--Below, I commented out some of the Maratis functions I might want to use in my motion functions
--[[translate(object,direction,"local")
onKeyDown(key)
onKeyUp(key)
setAnimationSpeed(object, speed)
playSound(object)
pauseSound(object)
stopSound(object)
getSoundGain(object)
setSoundGain(object, gain)]]
--These are all of the motion variables
move_object = box
move_speed = .1
--These are the various DIRECTIONS in which you can move. They are Vector3 variables.
--A vector3(vec3) is just a spot in 3D space.
--I used a variable inside of the vector3 so that I can change it outside of the vec3 function.
--[[
The reason that I made one negative and positive inside of the vec3 function is so that when
I change it outside of the function I don't have to make it negative (I can just use positive values)
]]
move_forward = vec3(x, -move_speed, z) --[1]
move_backward = vec3(x, move_speed, z) --[2]
move_left = vec3(move_speed, y, z) --[3]
move_right = vec3(-move_speed, y, z) --[4]
move_down= vec3(x, y,-move_speed) --[5]
move_up= vec3(x, y, move_speed) --[6]
--This table/array holds all of the directions so that I don't have to keep type out every direction. Instead of:
--[[
move_forward
move_backward
move_right
move_down
move_up
]]
--I can type:
--[[
move_direction[1]
move_direction[2]
etc
]]
move_direction = {move_forward,move_backward,move_left,move_right,move_down,move_up}
--This is another table for choosing which buttons are used to move something
--I can quickly change what button moves an object up by switching out the "W" to "UP"
move_btn = {move_btn_U,move_btn_D,move_btn_L,move_btn,R}
move_btn_U = "W"
move_btn_D = "S"
move_btn_L = "LEFT"
move_btn_R = "RIGHT"
--These variables deterine animations that are played, as well as the speed at which they are played.
-- I don't have to dig through functions to simply adjust the speed at whcih the animation plays
move_animation = 1
idle_animation = 0
move_animation_speed = 2
idle_animation_speed = .5
--The sound made when moving
move_sound = steps
--JUMP VARIABLES
--[[translate(object,direction,"local") ]]
--The Jump area works just like the Move area.
col = getNumCollisions(jump_object)
jump_object = box
jump_height = 3
jump_speed = 1
jump_btn = "SPACE"
jump_animation = changeAnimation(object, animationId)
jump_direction = {jump_up,jump_down,jump,left,jump_right}
jump_up = vec3(x, y, jump_speed*jump_height)
jump_down = vec3(x, y, jump_speed*jump_height)
jump_left = vec3(-jump_speed*jump_height, y, z)
jump_right = vec3(jump_speed*jump_height, y, z)
collisions = getNumCollisions(box)
--SPIN VARIABLES
--[[rotate(object,direction,angle,"local")]]
--Same as the Move area
spin_speed = 7
spin_object = box
spin_direction = {spin_left,spin_right}
spin_left = vec3(x, y, spin_speed)
spin_right = vec3(x, y, -spin_speed)
spin_btn = {spin_btn_L,spin_btn_R}
spin_btn_L = "A"
spin_btn_R = "D"
spin_animation = changeAnimation(object, animationId)
--MOVE FUNCTION
--All of the variables above are substituted into their respective functions.
--The goals is to have NO VALUES inside of the function itself as it simply controls variables.
--This way we can adjust variables outside of the function and have easy access to them.
--Also, complete and descriptive naming conventions are a MUST for clarity
function Move(move_object,move_direction,move_btn,move_animation,idle_animation)
if isKeyPressed(move_btn) then
changeAnimation(move_object,move_animation)
translate(move_object,move_direction,"local")
playSound(move_sound)
setAnimationSpeed(move_object, move_animation_speed)
end
if onKeyDown(move_btn) then
playSound(move_sound)
end
if onKeyUp(move_btn) then
changeAnimation(move_object,idle_animation)
setAnimationSpeed(move_object, idle_animation_speed)
stopSound(move_sound)
end
end
--JUMP FUNCTION
function Jump(jump_object,jump_direction,jump_btn--[[,jump_animation]])
if isKeyPressed(jump_btn) then
--changeAnimation(jump_object,jump_animation)
addCentralForce(jump_object, jump_direction, "local")
end
end
--SPIN FUNCTION
function Spin(spin_object,spin_direction,spin_btn)
if isKeyPressed(spin_btn) then
--changeAnimation(spin_object,spin_animation)
rotate(spin_object,spin_direction,spin_speed,"local")
end
end
----END MOTION
----ARTIFICIAL INTELLIGENCE
--This section works like the Motion section also.
--[[
So far I am trying to simulate the 5 senses. All of the senses are physics base and use basic collision to determine if something
is either SEEN or HEARD etc.
]]
--SEE VARIABLES
--[[isCollisionTest(object)
isCollisionBetween(object1, object2)
getNumCollisions(object)
setScale(object, {x, y, z})
changeAnimation(object, animationId)
setAnimationSpeed(object, speed)]]
--The SEEING section is a COLLISION TEST. A cone is representive of the eyes.
--object that can be seen
seen_object = monkey
--object that is seeing
seeing_object = cone
--A boolean to see if something is seen (if there was a collision between the seen and seeing object)
seen = isCollisionBetween(seeing_object,seen_object)--true or false
--Field of view. Basically the width of the cone
see_FOV = 0
--Length of the cone
see_distance = 0
--Animation to be played when SEEN is TRUE
see_animation = 0--animatinoID
--SMELL VARIABLES
--The smelling section is also COLLISION BASED. However, it detects collisions with a particle system
smelled = isCollisionTest(object)--true or false
smell_particle_system =
smell_animation = 0--animatinoID
--HEAR VARIABLES
--[[playSound(object)
--pauseSound(object)
--stopSound(object)
--getSoundGain(object)
--setSoundGain(object, gain)]]
--The hearing section is not physics based. It detects whether or not the volume of a sound is above a certain number. If it is, it is HEARD.
heard = false
hear_sound = getSoundGain(object)
hear_proximity = 0
hear_pitch = 0
hear_animation = 0--animationID
hear_limit = 3 --limit at which sound can be heard
--TOUCH VARIABLES
--Phsycs based.
touched = isCollisionTest(object)
touch_object = getObject("finish_line")
touch_animation = 0--animationID
--TASTE VARIABLES
--Physics based
tasted = isCollisionTest(object)
taste_object = getObject(« objectName »)
taste_proximity = 0
taste_animation = 0
--SEE FUNCTION
function See(seeing_object,seen_object)
if isCollisionBetween(seeing_object,seen_object) then
quit()
end
end
--HEAR FUNCTION
--the object in parenthesis is the object being heard (a sound object)
function Hear()
if heard and hear_sound > hear_limit then
changeAnimation(object,hear_animation)
end
end
--TOUCH FUNCTION
function Touch()
if touched then
changeAnimation(object,touch_animation)
end
end
--TASTE FUNCTION
function Taste()
if tasted then
changeAnimation(object,taste_animation)
tasted=true
end
end
--SMELL FUNCTION
function Smell()
if smelled then
changeAnimation(object,smell_animation)
smelled=true
end
end
----END ARTIFICIAL INTELLIGENCE
----DEFAULTS
changeAnimation(person,0)
stopSound(steps)
----END DEFAULTS
----ADDITIONAL FUNCTIONS
function GetText()
--Apples
--apples = {apple_clone,apple_clone,apple_clone,apple_clone}
--apples_collected = 0
--scale = vec3(2,2,2)
GuiScene = getScene("Scene_name")
Camera = getObject("Camera_name")
enableCameraLayer(Camera, GuiScene)
text = getObject(GuiScene,"Text_object")
show_apples_collected = "Apples = " .. apples_collected
setText(text,show_apples_collected)
--Collect(apple)
--Collect(apple_clone)
--coll = getNumCollisions(Feet)
end
--[[COLLECTION FUNCTION
function Collect(object)
if isCollisionBetween(Player,object) then
deactivate(object)
playSound(snd_collect)
--Scale()
apples_collected = apples_collected + 1
end
end
]]
----END OF ADDITIONAL FUNCTIONS
----GAME
function onSceneUpdate()
--Move Box
Move(box,move_direction[1],move_btn_U)
Move(box,move_direction[2],move_btn_D)
Move(box,move_direction[3],move_btn_L)
Move(box,move_direction[4],move_btn_R)
--Jump Box
Jump(box,jump_up,jump_btn)
--Spin Box
Spin(box,spin_left,spin_btn_L)
Spin(box,spin_right,spin_btn_R)
----------------------------------------------------------------
--Move Person
Move(person,move_direction[1],move_btn_U,move_animation,idle_animation)
Move(person,move_direction[2],move_btn_D,move_animation,idle_animation)
Move(person,move_direction[3],move_btn_L,move_animation,idle_animation)
Move(person,move_direction[4],move_btn_R,move_animation,idle_animation)
--Jump Person
Jump(person,jump_up,jump_btn)
--Spin Person
Spin(person,spin_left,spin_btn_L)
Spin(person,spin_right,spin_btn_R)
end
----END GAME
The difference between the SEEING OBJECT and the TASTING OBJECT and the FEELING OBJECT is simply the SHAPE of the representative geometry. Seeing object is a cone. Tasting object can be a cube if you want. Feeling Object can be a cylinder etc. Whatever geometry best portrays the sense. Cone is best for sight.
These objects can also be parented to different objects and in various locations on the object so that the object itself will only be able to, for example, TOUCH something that has collided with it's RIGHT SHOE. hehe