couch/demo/exampleworld/main.lua

234 lines
7.6 KiB
Lua
Raw Normal View History

2021-01-15 20:33:25 -06:00
local min = math.min
local max = math.max
2021-01-24 16:37:35 -06:00
local cube
local physics_ball
local character
2021-01-13 16:47:16 -06:00
local ball
local camera
local die_cube
local total = 0.0
2021-01-13 16:47:16 -06:00
2021-01-13 18:51:58 -06:00
local vx = 0.0
2021-01-23 10:19:47 -06:00
local vy = 0.0
2021-01-13 18:51:58 -06:00
local vz = 0.0
local character_move_vec = couch.Vector3(0.0, 0.0, 0.0)
2021-01-13 18:51:58 -06:00
local ballvy = -1.0
2021-01-13 20:50:08 -06:00
local cam_rot_x = 0.0
2021-01-13 22:50:01 -06:00
local cam_rot_y = 0.0
2021-01-13 20:50:08 -06:00
2021-01-15 20:33:25 -06:00
local SPEED = 30
local WHITE = couch.Vector3(1.0, 1.0, 1.0)
local RED = couch.Vector3(1.0, 0.0, 0.0)
local BLUE = couch.Vector3(0.0, 0.0, 1.0)
2021-01-21 15:26:39 -06:00
local light
2021-01-13 16:47:16 -06:00
function init()
2021-01-20 14:54:54 -06:00
local material
2021-01-26 23:28:20 -06:00
local transform
2021-01-20 14:54:54 -06:00
2021-01-13 16:47:16 -06:00
camera = couch.Camera()
camera:MakeCurrent()
2021-01-26 23:28:20 -06:00
camera:Translate(couch.Vector3(0.0, 0.0, 10.0))
2021-01-21 15:26:39 -06:00
2021-01-24 16:37:35 -06:00
local light = couch.DirectionalLight()
2021-03-04 10:41:45 -06:00
light:SetDirection(couch.Vector3(0.0, -1.0, -1.0))
2021-03-02 11:48:18 -06:00
light:SetColor(couch.Vector3(1.0, 1.0, 1.0))
light:SetAmbient(0.2)
light:SetDiffuse(1.0)
light:SetSpecular(0.1)
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(light:Instance())
2021-01-23 11:54:34 -06:00
2021-01-24 16:37:35 -06:00
local skybox = couch.Skybox.FromFiles(
2021-01-30 19:39:25 -06:00
"../resources/skybox/px.png",
"../resources/skybox/nx.png",
"../resources/skybox/py.png",
"../resources/skybox/ny.png",
"../resources/skybox/pz.png",
"../resources/skybox/nz.png"
2021-01-23 11:54:34 -06:00
)
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(skybox:Instance())
2021-01-23 13:00:08 -06:00
2021-01-24 22:55:36 -06:00
local physics_ball_prefab = couch.Rigidbody()
2021-01-30 19:39:25 -06:00
local physics_ball_mesh = couch.Mesh.FromFile("../resources/ball.obj")
2021-01-24 22:55:36 -06:00
material = physics_ball_mesh:GetMaterial(0)
material.ambient = BLUE
material.diffuse = BLUE
physics_ball_mesh:SetMaterial(0, material)
2021-01-26 16:42:28 -06:00
physics_ball_prefab:AddChild(physics_ball_mesh);
2021-01-30 19:39:25 -06:00
physics_ball_prefab:Translate(couch.Vector3(-14.0, 30.0, -5.0))
physics_ball = physics_ball_prefab:Instance()
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(physics_ball)
2021-01-24 22:55:36 -06:00
2021-01-23 13:00:08 -06:00
make_ground()
2021-01-24 16:37:35 -06:00
2021-01-30 19:39:25 -06:00
local character_prefab = couch.Mesh.FromFile("../resources/capsule.obj")
material = character_prefab:GetMaterial(0)
material.ambient = BLUE
material.diffuse = BLUE
material.specular = WHITE * 0.1
character_prefab:SetMaterial(0, material)
local character_body = couch.Rigidbody()
2021-01-27 00:01:17 -06:00
character_body:SetMass(1.0)
character_body:SetCollisionShape(couch.CapsuleCollisionShape(1.0, 1.0))
character_body:SetCharacter(true)
2021-01-26 16:42:28 -06:00
character_body:AddChild(character_prefab)
2021-01-30 19:39:25 -06:00
character_body:Translate(couch.Vector3(-15.0, 3.0, 0.0))
character = character_body:Instance()
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(character)
2021-01-30 19:39:25 -06:00
local cube_prefab = couch.Mesh.FromFile("../resources/cube.obj")
2021-01-24 16:37:35 -06:00
material = cube_prefab:GetMaterial(0)
2021-01-22 20:12:15 -06:00
material.ambient = RED
material.diffuse = RED
2021-01-24 16:37:35 -06:00
cube_prefab:SetMaterial(0, material)
2021-01-30 19:39:25 -06:00
local orbiter = couch.Mesh.FromFile("../resources/ball.obj")
2021-01-26 23:28:20 -06:00
orbiter:UniformScale(0.25);
orbiter:Translate(couch.Vector3(1.0, 0.0, 0.0))
2021-01-26 16:42:28 -06:00
cube_prefab:AddChild(orbiter)
2021-01-24 16:37:35 -06:00
cube = cube_prefab:Instance()
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(cube)
die_cube = cube_prefab:Instance()
couch.Node.GetRoot():AddChild(die_cube)
die_cube:Translate(couch.Vector3(0.0, 0.0, 3.0))
2021-01-30 19:39:25 -06:00
local ball_prefab = couch.Mesh.FromFile("../resources/ball.obj")
2021-01-24 16:37:35 -06:00
material = ball_prefab:GetMaterial(0)
ball_prefab:SetMaterial(0, material)
ball = ball_prefab:Instance()
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(ball)
2021-01-13 18:51:58 -06:00
2021-01-26 23:28:20 -06:00
ball:Translate(couch.Vector3(0.0, 3.0, 0.0))
2021-01-18 18:25:47 -06:00
2021-01-30 19:39:25 -06:00
local trough_prefab = couch.TexturedMesh("../resources/trough.obj", "../resources/wood_lowres.png")
2021-01-24 16:37:35 -06:00
trough = trough_prefab:Instance()
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(trough)
2021-01-26 23:28:20 -06:00
trough:Translate(couch.Vector3(10.0, 0.0, 0.0))
2021-01-20 12:50:59 -06:00
2021-01-30 19:39:25 -06:00
local scaffold_prefab = couch.TexturedMesh("../resources/scaffold.obj", "../resources/grate_floor_lowres.png", "../resources/railing.png")
2021-01-24 16:37:35 -06:00
local scaffold = scaffold_prefab:Instance()
2021-01-20 20:49:12 -06:00
material = scaffold:GetMaterial(0)
material.alphaScissor = 0.9
scaffold:SetMaterial(0, material)
2021-01-22 15:27:32 -06:00
material = scaffold:GetMaterial(1)
material.cullBack = false
scaffold:SetMaterial(1, material)
2021-01-20 20:49:12 -06:00
material = scaffold:GetMaterial(1)
material.alphaScissor = 0.1
scaffold:SetMaterial(1, material)
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(scaffold)
2021-01-26 23:28:20 -06:00
scaffold:Translate(couch.Vector3(-3.0, 3.0, 0.0))
2021-01-22 20:32:45 -06:00
2021-01-30 19:39:25 -06:00
local barn_prefab = couch.TexturedMesh("../resources/barn.obj", "../resources/paintedwood.jpg", "../resources/barnroof_lowres.png", "../resources/wood_lowres.png")
material = barn_prefab:GetMaterial(0)
2021-01-22 20:32:45 -06:00
material.cullBack = false
2021-01-30 19:39:25 -06:00
barn_prefab:SetMaterial(0, material)
material = barn_prefab:GetMaterial(1)
2021-01-23 10:19:47 -06:00
material.cullBack = false
2021-01-30 19:39:25 -06:00
barn_prefab:SetMaterial(1, material)
local barn_body = couch.Rigidbody()
barn_body:SetCollisionShape(couch.MeshCollisionShape(barn_prefab))
barn_body:SetMass(0.0)
barn_body:AddChild(barn_prefab)
local barn = barn_body:Instance()
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(barn)
2021-01-26 23:28:20 -06:00
barn:Translate(couch.Vector3(-15.0, 0.0, 0.0))
2021-01-13 16:47:16 -06:00
end
function update(delta)
total = total + delta
2021-01-15 20:33:25 -06:00
local move_vec = couch.Vector3()
2021-01-26 23:28:20 -06:00
local camera_transform = camera:GetTransform()
move_vec = camera_transform.position + camera_transform:Forward() * delta * vz * SPEED
move_vec = move_vec + camera_transform:Right() * delta * vx * SPEED
move_vec = move_vec + camera_transform:Up() * delta * vy * SPEED
camera_transform.position = move_vec
2021-01-15 20:33:25 -06:00
2021-01-26 23:28:20 -06:00
camera_transform.rotation.y = camera_transform.rotation.y - cam_rot_x * delta
2021-01-13 22:50:01 -06:00
cam_rot_x = 0.0
2021-01-26 23:28:20 -06:00
camera_transform.rotation.x = camera_transform.rotation.x - cam_rot_y * delta
camera_transform.rotation.x = min(max(camera_transform.rotation.x, -3.14 / 2.0), 3.14 / 2.0)
2021-01-13 22:50:01 -06:00
cam_rot_y = 0.0
2021-01-13 18:51:58 -06:00
2021-01-26 23:28:20 -06:00
camera:SetTransform(camera_transform)
local loc = ball:GetTransform().position
2021-01-13 18:51:58 -06:00
if loc.y > 4.0 then
ballvy = -1.0
elseif loc.y < 2.0 then
ballvy = 1.0
end
ball:Translate(couch.Vector3(0.0, ballvy * delta, 0.0))
2021-01-13 20:08:39 -06:00
2021-01-26 23:28:20 -06:00
cube:RotateY(2.0 * delta)
cube:RotateZ(1.0 * delta)
character:ApplyForce(character_move_vec * 10.0)
if total > 1.0 and die_cube then
die_cube:QueueFree()
die_cube = nil
end
2021-01-13 18:51:58 -06:00
end
2021-01-23 10:19:47 -06:00
function action_dir(key, action, pos, neg, curr)
if key == pos and action == couch.ACTION_PRESS then
return 1.0
elseif key == neg and action == couch.ACTION_PRESS then
return -1.0
elseif (key == pos or key == neg) and action == couch.ACTION_RELEASE then
return 0.0
else
return curr
2021-01-21 11:12:45 -06:00
end
2021-01-23 10:19:47 -06:00
end
2021-01-21 15:26:39 -06:00
2021-01-23 10:19:47 -06:00
function onkey(key, code, action, mod)
vz = action_dir(key, action, couch.KEY_W, couch.KEY_S, vz)
vx = action_dir(key, action, couch.KEY_D, couch.KEY_A, vx)
vy = action_dir(key, action, couch.KEY_SPACE, couch.KEY_LEFT_CONTROL, vy)
if key == couch.KEY_J and action == couch.ACTION_PRESS then
physics_ball:ApplyImpulse(couch.Vector3(0.0, 1.0, 0.0) * 10)
2021-01-21 15:26:39 -06:00
end
character_move_vec.z = action_dir(key, action, couch.KEY_DOWN, couch.KEY_UP, character_move_vec.z)
2021-01-13 16:47:16 -06:00
end
2021-01-13 22:50:01 -06:00
function onmousemotion(_, _, relx, rely)
cam_rot_x = relx
cam_rot_y = rely
end
2021-01-23 13:00:08 -06:00
function make_ground()
2021-01-30 19:39:25 -06:00
local ground_prefab = couch.TexturedMesh("../resources/ground.obj", "../resources/grass_lowres.png")
2021-01-26 23:28:20 -06:00
local ground_prefab_transform = ground_prefab:GetTransform()
ground_prefab_transform.position = couch.Vector3(0.0, -2.0, 0.0)
ground_prefab_transform.scale = couch.Vector3(3.0, 1.0, 3.0)
ground_prefab:SetTransform(ground_prefab_transform)
2021-01-23 13:03:22 -06:00
2021-01-24 16:37:35 -06:00
ground = couch.Spatial():Instance()
2021-01-26 16:42:28 -06:00
couch.Node.GetRoot():AddChild(ground)
2021-01-23 13:00:08 -06:00
-- Add a collisionshape
local ground_shape_prefab = couch.Rigidbody()
2021-01-27 00:01:17 -06:00
ground_shape_prefab:SetMass(0.0)
ground_shape_prefab:SetCollisionShape(couch.BoxCollisionShape(180.0, 1.0, 180.0))
2021-01-26 23:28:20 -06:00
ground_shape_prefab:Translate(couch.Vector3(0.0, -2.5, 0.0))
2021-01-26 16:42:28 -06:00
ground:AddChild(ground_shape_prefab:Instance())
2021-01-23 13:00:08 -06:00
for x = -20, 20, 1 do
for z = -20, 20, 1 do
2021-01-24 16:37:35 -06:00
local piece = ground_prefab:Instance()
2021-01-26 23:28:20 -06:00
piece:Translate(couch.Vector3(6.0 * x, -2.0, 6.0 * z))
2021-01-26 16:42:28 -06:00
ground:AddChild(piece)
2021-01-23 13:00:08 -06:00
end
end
end