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
|
2021-01-25 15:17:32 -06:00
|
|
|
local physics_ball
|
|
|
|
local character
|
2021-01-13 16:47:16 -06:00
|
|
|
local ball
|
|
|
|
local camera
|
|
|
|
|
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
|
|
|
|
|
2021-01-25 15:17:32 -06:00
|
|
|
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
|
|
|
|
|
2021-01-25 15:17:32 -06:00
|
|
|
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-17 14:36:38 -06:00
|
|
|
|
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-13 16:47:16 -06:00
|
|
|
camera = couch.Camera()
|
|
|
|
camera:MakeCurrent()
|
2021-01-13 20:08:39 -06:00
|
|
|
camera.transform:Translate(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-01-23 10:19:47 -06:00
|
|
|
light.direction = couch.Vector3(0.0, -1.0, -1.0)
|
2021-01-21 15:26:39 -06:00
|
|
|
light.color = couch.Vector3(1.0, 1.0, 1.0)
|
2021-01-21 18:03:50 -06:00
|
|
|
light.ambient = 0.2
|
2021-01-21 15:26:39 -06:00
|
|
|
light.diffuse = 1.0
|
2021-01-22 20:12:15 -06:00
|
|
|
light.specular = 0.1
|
2021-01-26 16:42:28 -06:00
|
|
|
print(couch.Node.GetRoot().GetChildren)
|
|
|
|
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-23 12:33:10 -06:00
|
|
|
"skybox/px.png",
|
|
|
|
"skybox/nx.png",
|
|
|
|
"skybox/py.png",
|
|
|
|
"skybox/ny.png",
|
|
|
|
"skybox/pz.png",
|
|
|
|
"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()
|
|
|
|
local physics_ball_mesh = couch.Mesh.FromFile("ball.obj")
|
|
|
|
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-24 22:55:36 -06:00
|
|
|
physics_ball_prefab.transform.position = couch.Vector3(0.0, 30.0, -10.0)
|
2021-01-25 15:17:32 -06:00
|
|
|
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-25 15:17:32 -06:00
|
|
|
local character_prefab = couch.Mesh.FromFile("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()
|
|
|
|
character_body.mass = 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-25 15:17:32 -06:00
|
|
|
character_body.transform.position = couch.Vector3(0.0, 3.0, 0.0)
|
|
|
|
character = character_body:Instance()
|
2021-01-26 16:42:28 -06:00
|
|
|
couch.Node.GetRoot():AddChild(character)
|
2021-01-25 15:17:32 -06:00
|
|
|
|
2021-01-24 16:37:35 -06:00
|
|
|
local cube_prefab = couch.Mesh.FromFile("cube.obj")
|
|
|
|
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-23 12:01:06 -06:00
|
|
|
local orbiter = couch.Mesh.FromFile("ball.obj")
|
|
|
|
orbiter.transform.scale = orbiter.transform.scale * 0.25;
|
|
|
|
orbiter.transform:Translate(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)
|
2021-01-20 17:05:59 -06:00
|
|
|
|
2021-01-24 16:37:35 -06:00
|
|
|
local ball_prefab = couch.Mesh.FromFile("ball.obj")
|
|
|
|
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-24 16:37:35 -06:00
|
|
|
ball.transform:Translate(0.0, 3.0, 0.0)
|
2021-01-18 18:25:47 -06:00
|
|
|
|
2021-01-24 16:37:35 -06:00
|
|
|
local trough_prefab = couch.TexturedMesh("trough.obj", "wood_lowres.png")
|
|
|
|
trough = trough_prefab:Instance()
|
2021-01-26 16:42:28 -06:00
|
|
|
couch.Node.GetRoot():AddChild(trough)
|
2021-01-18 19:06:37 -06:00
|
|
|
trough.transform:Translate(10.0, 0.0, 0.0)
|
2021-01-20 12:50:59 -06:00
|
|
|
|
2021-01-24 16:37:35 -06:00
|
|
|
local scaffold_prefab = couch.TexturedMesh("scaffold.obj", "grate_floor_lowres.png", "railing.png")
|
|
|
|
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-23 10:19:47 -06:00
|
|
|
scaffold.transform:Translate(-3.0, 3.0, 0.0)
|
2021-01-22 20:32:45 -06:00
|
|
|
|
2021-01-24 16:37:35 -06:00
|
|
|
local barn_prefab = couch.TexturedMesh("barn.obj", "paintedwood.jpg", "barnroof_lowres.png", "wood_lowres.png")
|
|
|
|
local barn = barn_prefab:Instance()
|
2021-01-22 20:32:45 -06:00
|
|
|
material = barn:GetMaterial(0)
|
|
|
|
material.cullBack = false
|
|
|
|
barn:SetMaterial(0, material)
|
2021-01-23 10:19:47 -06:00
|
|
|
material = barn:GetMaterial(1)
|
|
|
|
material.cullBack = false
|
|
|
|
barn:SetMaterial(1, material)
|
2021-01-26 16:42:28 -06:00
|
|
|
couch.Node.GetRoot():AddChild(barn)
|
2021-01-23 10:19:47 -06:00
|
|
|
barn.transform:Translate(-15.0, 0.0, 0.0)
|
2021-01-13 16:47:16 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function update(delta)
|
2021-01-15 20:33:25 -06:00
|
|
|
local move_vec = couch.Vector3()
|
2021-01-23 10:19:47 -06:00
|
|
|
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
|
2021-01-15 20:33:25 -06:00
|
|
|
camera.transform.position = move_vec
|
|
|
|
|
2021-01-13 22:50:01 -06:00
|
|
|
camera.transform.rotation.y = camera.transform.rotation.y - cam_rot_x * delta
|
|
|
|
cam_rot_x = 0.0
|
|
|
|
camera.transform.rotation.x = camera.transform.rotation.x - cam_rot_y * delta
|
2021-01-20 22:17:33 -06:00
|
|
|
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-24 16:37:35 -06:00
|
|
|
local loc = ball.transform.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
|
2021-01-24 16:37:35 -06:00
|
|
|
ball.transform.position.y = ball.transform.position.y + ballvy * delta
|
2021-01-13 20:08:39 -06:00
|
|
|
|
2021-01-24 16:37:35 -06:00
|
|
|
cube.transform.rotation.y = cube.transform.rotation.y + 2.0 * delta;
|
|
|
|
cube.transform.rotation.z = cube.transform.rotation.z + 1.0 * delta;
|
2021-01-25 15:17:32 -06:00
|
|
|
|
|
|
|
character:ApplyForce(character_move_vec * 10.0)
|
|
|
|
print(character_move_vec.z)
|
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)
|
2021-01-25 15:17:32 -06:00
|
|
|
|
|
|
|
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
|
2021-01-25 15:17:32 -06:00
|
|
|
|
|
|
|
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-23 13:03:22 -06:00
|
|
|
local ground_prefab = couch.TexturedMesh("ground.obj", "grass_lowres.png")
|
|
|
|
ground_prefab.transform.position = couch.Vector3(0.0, -2.0, 0.0)
|
|
|
|
ground_prefab.transform.scale = couch.Vector3(3.0, 1.0, 3.0)
|
|
|
|
|
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
|
|
|
|
2021-01-25 15:17:32 -06:00
|
|
|
-- Add a collisionshape
|
|
|
|
local ground_shape_prefab = couch.Rigidbody()
|
|
|
|
ground_shape_prefab.mass = 0.0
|
|
|
|
ground_shape_prefab:SetCollisionShape(couch.BoxCollisionShape(180.0, 1.0, 180.0))
|
|
|
|
ground_shape_prefab.transform:Translate(0.0, -2.5, 0.0)
|
2021-01-26 16:42:28 -06:00
|
|
|
ground:AddChild(ground_shape_prefab:Instance())
|
2021-01-25 15:17:32 -06:00
|
|
|
|
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-23 23:39:13 -06:00
|
|
|
piece.transform.position = 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
|