2014-10-12 10:06:07 +03:00
|
|
|
-- Buildat: digger/client_lua/init.lua
|
|
|
|
-- http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
-- Copyright 2014 Perttu Ahola <celeron55@gmail.com>
|
|
|
|
-- Copyright 2014 Břetislav Štec <valsiterb@gmail.com>
|
|
|
|
local log = buildat.Logger("digger")
|
|
|
|
local dump = buildat.dump
|
|
|
|
local cereal = require("buildat/extension/cereal")
|
|
|
|
local magic = require("buildat/extension/urho3d")
|
|
|
|
local replicate = require("buildat/extension/replicate")
|
2014-10-12 10:11:36 +03:00
|
|
|
local voxelworld = require("buildat/module/voxelworld")
|
2014-10-12 10:06:07 +03:00
|
|
|
|
2014-10-15 11:57:24 +03:00
|
|
|
--local RENDER_DISTANCE = 640
|
2014-10-15 04:14:20 +03:00
|
|
|
local RENDER_DISTANCE = 480
|
2014-10-15 00:59:09 +03:00
|
|
|
--local RENDER_DISTANCE = 320
|
|
|
|
--local RENDER_DISTANCE = 240
|
2014-10-15 04:14:20 +03:00
|
|
|
--local RENDER_DISTANCE = 160
|
|
|
|
|
|
|
|
local FOG_END = RENDER_DISTANCE * 1.2
|
2014-10-14 12:23:51 +03:00
|
|
|
|
2014-10-15 16:09:10 +03:00
|
|
|
local PLAYER_HEIGHT = 1.7
|
2014-10-13 01:32:58 +03:00
|
|
|
local PLAYER_WIDTH = 0.9
|
|
|
|
local MOVE_SPEED = 10
|
|
|
|
local JUMP_SPEED = 7 -- Barely 2 voxels
|
|
|
|
|
2014-10-12 10:06:07 +03:00
|
|
|
local scene = replicate.main_scene
|
|
|
|
|
2014-10-21 15:24:11 +03:00
|
|
|
local pointed_voxel_p = nil
|
|
|
|
local pointed_voxel_p_above = nil
|
|
|
|
local pointed_voxel_visual_node = scene:CreateChild("node")
|
|
|
|
local pointed_voxel_visual_geometry =
|
|
|
|
pointed_voxel_visual_node:CreateComponent("CustomGeometry")
|
|
|
|
do
|
|
|
|
pointed_voxel_visual_geometry:BeginGeometry(0, magic.TRIANGLE_LIST)
|
|
|
|
pointed_voxel_visual_geometry:SetNumGeometries(1)
|
2014-10-21 17:51:42 +03:00
|
|
|
local function define_face(lc, uc, color)
|
|
|
|
pointed_voxel_visual_geometry:DefineVertex(
|
|
|
|
magic.Vector3(lc.x, uc.y, uc.z))
|
|
|
|
pointed_voxel_visual_geometry:DefineColor(color)
|
|
|
|
pointed_voxel_visual_geometry:DefineVertex(
|
|
|
|
magic.Vector3(uc.x, uc.y, uc.z))
|
|
|
|
pointed_voxel_visual_geometry:DefineColor(color)
|
|
|
|
pointed_voxel_visual_geometry:DefineVertex(
|
|
|
|
magic.Vector3(uc.x, uc.y, lc.z))
|
|
|
|
pointed_voxel_visual_geometry:DefineColor(color)
|
|
|
|
pointed_voxel_visual_geometry:DefineVertex(
|
|
|
|
magic.Vector3(uc.x, uc.y, lc.z))
|
|
|
|
pointed_voxel_visual_geometry:DefineColor(color)
|
|
|
|
pointed_voxel_visual_geometry:DefineVertex(
|
|
|
|
magic.Vector3(lc.x, uc.y, lc.z))
|
|
|
|
pointed_voxel_visual_geometry:DefineColor(color)
|
|
|
|
pointed_voxel_visual_geometry:DefineVertex(
|
|
|
|
magic.Vector3(lc.x, uc.y, uc.z))
|
|
|
|
pointed_voxel_visual_geometry:DefineColor(color)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Full face
|
|
|
|
--local d = 0.502
|
|
|
|
--local c = magic.Color(0.04, 0.12, 0.04)
|
|
|
|
--define_face(magic.Vector3(-d, d, -d), magic.Vector3(d, d, d), c)
|
|
|
|
--pointed_voxel_visual_geometry:Commit()
|
|
|
|
--local m = magic.Material.new()
|
|
|
|
--m:SetTechnique(0, magic.cache:GetResource("Technique",
|
|
|
|
-- "Techniques/NoTextureVColAdd.xml"))
|
|
|
|
|
|
|
|
-- Face edges
|
2014-10-21 15:24:11 +03:00
|
|
|
local d = 0.502
|
2014-10-21 17:56:23 +03:00
|
|
|
local d2 = 1.0 / 16
|
|
|
|
local c = magic.Color(0.12, 0.36, 0.12)
|
2014-10-21 17:51:42 +03:00
|
|
|
define_face(magic.Vector3(-d, d, d-d2), magic.Vector3(d, d, d), c)
|
|
|
|
define_face(magic.Vector3(-d, d, -d), magic.Vector3(d, d, -d+d2), c)
|
2014-10-21 17:56:23 +03:00
|
|
|
define_face(magic.Vector3(d-d2, d, -d+d2), magic.Vector3(d, d, d-d2), c)
|
|
|
|
define_face(magic.Vector3(-d, d, -d+d2), magic.Vector3(-d+d2, d, d-d2), c)
|
2014-10-21 15:24:11 +03:00
|
|
|
pointed_voxel_visual_geometry:Commit()
|
|
|
|
local m = magic.Material.new()
|
|
|
|
m:SetTechnique(0, magic.cache:GetResource("Technique",
|
2014-10-21 17:51:42 +03:00
|
|
|
"Techniques/NoTextureVColMultiply.xml"))
|
|
|
|
|
2014-10-21 15:24:11 +03:00
|
|
|
pointed_voxel_visual_geometry:SetMaterial(0, m)
|
|
|
|
pointed_voxel_visual_node.enabled = false
|
|
|
|
end
|
|
|
|
|
2014-10-13 01:07:09 +03:00
|
|
|
magic.input:SetMouseVisible(false)
|
|
|
|
|
2014-10-19 20:27:09 +03:00
|
|
|
-- Set up zone (global visual parameters)
|
2014-10-20 14:05:58 +03:00
|
|
|
---[[
|
2014-10-19 23:39:35 +03:00
|
|
|
do
|
|
|
|
local zone_node = scene:CreateChild("Zone")
|
|
|
|
local zone = zone_node:CreateComponent("Zone")
|
|
|
|
zone.boundingBox = magic.BoundingBox(-1000, 1000)
|
|
|
|
zone.ambientColor = magic.Color(0.1, 0.1, 0.1)
|
|
|
|
--zone.ambientColor = magic.Color(0, 0, 0)
|
|
|
|
zone.fogColor = magic.Color(0.6, 0.7, 0.8)
|
|
|
|
--zone.fogColor = magic.Color(0, 0, 0)
|
|
|
|
zone.fogStart = 10
|
|
|
|
zone.fogEnd = FOG_END
|
2014-10-20 17:15:32 +03:00
|
|
|
zone.priority = -1
|
2014-10-21 01:47:51 +03:00
|
|
|
zone.override = true
|
2014-10-19 23:39:35 +03:00
|
|
|
end
|
|
|
|
--]]
|
2014-10-13 01:32:58 +03:00
|
|
|
|
2014-10-19 20:27:09 +03:00
|
|
|
-- Add lights
|
2014-10-19 23:39:35 +03:00
|
|
|
do
|
|
|
|
--[[
|
|
|
|
local dirs = {
|
|
|
|
magic.Vector3( 1.0, -1.0, 1.0),
|
|
|
|
magic.Vector3( 1.0, -1.0, -1.0),
|
|
|
|
magic.Vector3(-1.0, -1.0, -1.0),
|
|
|
|
magic.Vector3(-1.0, -1.0, 1.0),
|
|
|
|
}
|
|
|
|
for _, dir in ipairs(dirs) do
|
|
|
|
local node = scene:CreateChild("DirectionalLight")
|
|
|
|
node.direction = dir
|
|
|
|
local light = node:CreateComponent("Light")
|
|
|
|
light.lightType = magic.LIGHT_DIRECTIONAL
|
|
|
|
light.castShadows = true
|
|
|
|
light.brightness = 0.2
|
|
|
|
light.color = magic.Color(0.7, 0.7, 1.0)
|
|
|
|
end
|
|
|
|
--]]
|
|
|
|
|
|
|
|
local node = scene:CreateChild("DirectionalLight")
|
|
|
|
node.direction = magic.Vector3(-0.6, -1.0, 0.8)
|
|
|
|
local light = node:CreateComponent("Light")
|
|
|
|
light.lightType = magic.LIGHT_DIRECTIONAL
|
|
|
|
light.castShadows = true
|
|
|
|
light.brightness = 0.8
|
|
|
|
light.color = magic.Color(1.0, 1.0, 0.95)
|
|
|
|
|
|
|
|
---[[
|
|
|
|
local node = scene:CreateChild("DirectionalLight")
|
|
|
|
node.direction = magic.Vector3(0.3, -1.0, -0.4)
|
|
|
|
local light = node:CreateComponent("Light")
|
|
|
|
light.lightType = magic.LIGHT_DIRECTIONAL
|
|
|
|
light.castShadows = true
|
|
|
|
light.brightness = 0.2
|
|
|
|
light.color = magic.Color(0.7, 0.7, 1.0)
|
|
|
|
--]]
|
|
|
|
|
|
|
|
--[[
|
|
|
|
local node = scene:CreateChild("DirectionalLight")
|
|
|
|
node.direction = magic.Vector3(0.0, -1.0, 0.0)
|
|
|
|
local light = node:CreateComponent("Light")
|
|
|
|
light.lightType = magic.LIGHT_DIRECTIONAL
|
|
|
|
light.castShadows = false
|
|
|
|
light.brightness = 0.05
|
|
|
|
light.color = magic.Color(1.0, 1.0, 1.0)
|
|
|
|
--]]
|
|
|
|
end
|
2014-10-19 20:27:09 +03:00
|
|
|
|
2014-10-13 01:07:09 +03:00
|
|
|
-- Add a node that the player can use to walk around with
|
|
|
|
local player_node = scene:CreateChild("Player")
|
2014-10-19 23:39:35 +03:00
|
|
|
local player_shape = player_node:CreateComponent("CollisionShape")
|
|
|
|
do
|
|
|
|
--player_node.position = magic.Vector3(0, 30, 0)
|
|
|
|
--player_node.position = magic.Vector3(55, 30, 40)
|
|
|
|
player_node.position = magic.Vector3(-5, 1, 257)
|
|
|
|
player_node.direction = magic.Vector3(-1, 0, 0.4)
|
|
|
|
--player_node:Yaw(-177.49858)
|
|
|
|
---[[
|
|
|
|
local body = player_node:CreateComponent("RigidBody")
|
|
|
|
--body.mass = 70.0
|
|
|
|
body.friction = 0
|
|
|
|
--body.linearVelocity = magic.Vector3(0, -10, 0)
|
|
|
|
body.angularFactor = magic.Vector3(0, 0, 0)
|
|
|
|
body.gravityOverride = magic.Vector3(0, -15.0, 0) -- A bit more than normally
|
|
|
|
--player_shape:SetBox(magic.Vector3(1, 1.7*PLAYER_SCALE, 1))
|
|
|
|
player_shape:SetCapsule(PLAYER_WIDTH, PLAYER_HEIGHT)
|
|
|
|
--]]
|
|
|
|
end
|
2014-10-13 01:07:09 +03:00
|
|
|
|
2014-10-13 01:32:58 +03:00
|
|
|
local player_touches_ground = false
|
2014-10-15 11:57:24 +03:00
|
|
|
local player_crouched = false
|
2014-10-13 01:32:58 +03:00
|
|
|
|
2014-10-13 01:07:09 +03:00
|
|
|
-- Add a camera so we can look at the scene
|
|
|
|
local camera_node = player_node:CreateChild("Camera")
|
2014-10-19 23:39:35 +03:00
|
|
|
do
|
|
|
|
camera_node.position = magic.Vector3(0, 0.411*PLAYER_HEIGHT, 0)
|
|
|
|
--camera_node:Pitch(13.60000)
|
|
|
|
local camera = camera_node:CreateComponent("Camera")
|
2014-10-21 13:28:13 +03:00
|
|
|
camera.nearClip = 0.5 * math.min(
|
|
|
|
PLAYER_WIDTH * 0.15,
|
|
|
|
PLAYER_HEIGHT * (0.5 - 0.411)
|
|
|
|
)
|
2014-10-19 23:39:35 +03:00
|
|
|
camera.farClip = RENDER_DISTANCE
|
|
|
|
camera.fov = 75
|
2014-10-12 10:06:07 +03:00
|
|
|
|
2014-10-19 23:39:35 +03:00
|
|
|
-- And this thing so the camera is shown on the screen
|
|
|
|
local viewport = magic.Viewport:new(scene, camera_node:GetComponent("Camera"))
|
|
|
|
magic.renderer:SetViewport(0, viewport)
|
|
|
|
end
|
2014-10-12 10:06:07 +03:00
|
|
|
|
2014-10-19 20:27:09 +03:00
|
|
|
-- Tell about the camera to the voxel world so it can do stuff based on the
|
|
|
|
-- camera's position and other properties
|
2014-10-12 10:11:36 +03:00
|
|
|
voxelworld.set_camera(camera_node)
|
|
|
|
|
2014-10-21 01:47:51 +03:00
|
|
|
---[[
|
2014-10-19 23:39:35 +03:00
|
|
|
-- Add a light to the camera
|
|
|
|
do
|
|
|
|
local node = camera_node:CreateChild("Light")
|
|
|
|
local light = node:CreateComponent("Light")
|
|
|
|
light.lightType = magic.LIGHT_POINT
|
|
|
|
light.castShadows = false
|
|
|
|
light.brightness = 0.15
|
2014-10-21 01:47:51 +03:00
|
|
|
light.color = magic.Color(1.0, 1.0, 1.0)
|
|
|
|
light.range = 15.0
|
|
|
|
light.fadeDistance = 15.0
|
2014-10-19 23:39:35 +03:00
|
|
|
end
|
|
|
|
--]]
|
|
|
|
|
2014-10-12 10:06:07 +03:00
|
|
|
-- Add some text
|
|
|
|
local title_text = magic.ui.root:CreateChild("Text")
|
2014-10-13 01:32:58 +03:00
|
|
|
local misc_text = magic.ui.root:CreateChild("Text")
|
2014-10-19 23:39:35 +03:00
|
|
|
do
|
|
|
|
title_text:SetText("digger/init.lua")
|
|
|
|
title_text:SetFont(magic.cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
|
|
|
|
title_text.horizontalAlignment = magic.HA_CENTER
|
|
|
|
title_text.verticalAlignment = magic.VA_CENTER
|
|
|
|
title_text:SetPosition(0, -magic.ui.root.height/2 + 20)
|
|
|
|
|
|
|
|
misc_text:SetText("")
|
|
|
|
misc_text:SetFont(magic.cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
|
|
|
|
misc_text.horizontalAlignment = magic.HA_CENTER
|
|
|
|
misc_text.verticalAlignment = magic.VA_CENTER
|
|
|
|
misc_text:SetPosition(0, -magic.ui.root.height/2 + 40)
|
|
|
|
end
|
2014-10-13 01:32:58 +03:00
|
|
|
|
2014-10-19 23:39:35 +03:00
|
|
|
-- Unfocus UI
|
2014-10-12 10:06:07 +03:00
|
|
|
magic.ui:SetFocusElement(nil)
|
|
|
|
|
2014-10-13 01:07:09 +03:00
|
|
|
magic.SubscribeToEvent("KeyDown", function(event_type, event_data)
|
2014-10-12 10:06:07 +03:00
|
|
|
local key = event_data:GetInt("Key")
|
|
|
|
if key == magic.KEY_ESC then
|
|
|
|
log:info("KEY_ESC pressed")
|
|
|
|
buildat.disconnect()
|
|
|
|
end
|
2014-10-13 01:07:09 +03:00
|
|
|
end)
|
|
|
|
|
2014-10-21 15:24:11 +03:00
|
|
|
-- Returns: nil or p_under, p_above: buildat.Vector3
|
|
|
|
-- TODO: This algorithm is quite wasteful
|
|
|
|
local function find_pointed_voxel(camera_node)
|
|
|
|
local max_d = 6
|
|
|
|
local d_per_step = 0.1
|
|
|
|
local p0 = buildat.Vector3(camera_node.worldPosition)
|
|
|
|
local dir = buildat.Vector3(camera_node.worldDirection)
|
|
|
|
local last_p = nil
|
|
|
|
--log:verbose("p0="..p0:dump()..", dir="..dir:dump())
|
|
|
|
for i = 1, math.floor(max_d / d_per_step) do
|
|
|
|
local p = (p0 + dir * i * d_per_step):round()
|
|
|
|
if p ~= last_p then
|
|
|
|
local v = voxelworld.get_static_voxel(p)
|
|
|
|
--log:verbose("i="..i..": p="..p:dump()..", v.id="..v.id)
|
|
|
|
if v.id ~= 1 and v.id ~= 0 then
|
|
|
|
return p, last_p
|
|
|
|
end
|
|
|
|
last_p = p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
2014-10-21 12:57:57 +03:00
|
|
|
end
|
|
|
|
|
2014-10-17 11:49:47 +03:00
|
|
|
magic.SubscribeToEvent("MouseButtonDown", function(event_type, event_data)
|
|
|
|
local button = event_data:GetInt("Button")
|
2014-10-19 20:02:33 +03:00
|
|
|
log:info("MouseButtonDown: "..button)
|
2014-10-19 20:14:49 +03:00
|
|
|
if button == magic.MOUSEB_RIGHT then
|
2014-10-21 15:24:11 +03:00
|
|
|
local p = pointed_voxel_p_above
|
|
|
|
if p then
|
|
|
|
local data = cereal.binary_output({
|
|
|
|
p = {
|
|
|
|
x = math.floor(p.x+0.5),
|
|
|
|
y = math.floor(p.y+0.5),
|
|
|
|
z = math.floor(p.z+0.5),
|
|
|
|
},
|
|
|
|
}, {"object",
|
|
|
|
{"p", {"object",
|
|
|
|
{"x", "int32_t"},
|
|
|
|
{"y", "int32_t"},
|
|
|
|
{"z", "int32_t"},
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
buildat.send_packet("main:place_voxel", data)
|
|
|
|
end
|
2014-10-17 11:49:47 +03:00
|
|
|
end
|
2014-10-19 20:14:49 +03:00
|
|
|
if button == magic.MOUSEB_LEFT then
|
2014-10-21 15:24:11 +03:00
|
|
|
local p = pointed_voxel_p
|
|
|
|
if p then
|
|
|
|
local data = cereal.binary_output({
|
|
|
|
p = {
|
|
|
|
x = math.floor(p.x+0.5),
|
|
|
|
y = math.floor(p.y+0.5 + 0.2),
|
|
|
|
z = math.floor(p.z+0.5),
|
|
|
|
},
|
|
|
|
}, {"object",
|
|
|
|
{"p", {"object",
|
|
|
|
{"x", "int32_t"},
|
|
|
|
{"y", "int32_t"},
|
|
|
|
{"z", "int32_t"},
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
buildat.send_packet("main:dig_voxel", data)
|
|
|
|
end
|
2014-10-19 20:14:49 +03:00
|
|
|
end
|
2014-10-21 12:57:57 +03:00
|
|
|
if button == magic.MOUSEB_MIDDLE then
|
|
|
|
local p = player_node.position
|
|
|
|
local v = voxelworld.get_static_voxel(p)
|
|
|
|
log:info("get_static_voxel("..buildat.Vector3(p):dump()..")"..
|
|
|
|
" returned v.id="..dump(v.id))
|
|
|
|
end
|
2014-10-17 11:49:47 +03:00
|
|
|
end)
|
|
|
|
|
2014-10-13 01:07:09 +03:00
|
|
|
magic.SubscribeToEvent("Update", function(event_type, event_data)
|
2014-10-13 01:32:58 +03:00
|
|
|
--log:info("Update")
|
2014-10-21 15:24:11 +03:00
|
|
|
|
|
|
|
if camera_node then
|
|
|
|
local p, p_above = find_pointed_voxel(camera_node)
|
|
|
|
pointed_voxel_p = p
|
|
|
|
pointed_voxel_p_above = p_above
|
|
|
|
if p then
|
|
|
|
local v = voxelworld.get_static_voxel(p)
|
|
|
|
--log:info("pointed voxel: "..p:dump()..": "..v.id)
|
|
|
|
pointed_voxel_visual_node.position = magic.Vector3.from_buildat(p)
|
|
|
|
local d = p_above - p
|
|
|
|
if d.x > 0 then
|
|
|
|
pointed_voxel_visual_node.rotation =
|
2014-10-21 17:51:42 +03:00
|
|
|
magic.Quaternion(90, 0, -90)
|
2014-10-21 15:24:11 +03:00
|
|
|
elseif d.x < 0 then
|
|
|
|
pointed_voxel_visual_node.rotation =
|
2014-10-21 17:51:42 +03:00
|
|
|
magic.Quaternion(90, 0, 90)
|
2014-10-21 15:24:11 +03:00
|
|
|
elseif d.y > 0 then
|
|
|
|
pointed_voxel_visual_node.rotation =
|
2014-10-21 17:51:42 +03:00
|
|
|
magic.Quaternion(0, 0, 0)
|
2014-10-21 15:24:11 +03:00
|
|
|
elseif d.y < 0 then
|
|
|
|
pointed_voxel_visual_node.rotation =
|
2014-10-21 17:51:42 +03:00
|
|
|
magic.Quaternion(0, 0, -180)
|
2014-10-21 15:24:11 +03:00
|
|
|
elseif d.z > 0 then
|
|
|
|
pointed_voxel_visual_node.rotation =
|
2014-10-21 17:51:42 +03:00
|
|
|
magic.Quaternion(90, 0, 0)
|
2014-10-21 15:24:11 +03:00
|
|
|
elseif d.z < 0 then
|
|
|
|
pointed_voxel_visual_node.rotation =
|
2014-10-21 17:51:42 +03:00
|
|
|
magic.Quaternion(-90, 0, 0)
|
2014-10-21 15:24:11 +03:00
|
|
|
end
|
|
|
|
pointed_voxel_visual_node.enabled = true
|
|
|
|
else
|
|
|
|
pointed_voxel_visual_node.enabled = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-13 01:07:09 +03:00
|
|
|
if player_node then
|
2014-10-21 13:52:09 +03:00
|
|
|
-- If falling out of world, restore onto world
|
|
|
|
if player_node.position.y < -500 then
|
|
|
|
player_node.position = magic.Vector3(0, 500, 0)
|
|
|
|
end
|
|
|
|
|
2014-10-13 01:07:09 +03:00
|
|
|
local dmouse = magic.input:GetMouseMove()
|
|
|
|
--log:info("dmouse: ("..dmouse.x..", "..dmouse.y..")")
|
|
|
|
camera_node:Pitch(dmouse.y * 0.1)
|
|
|
|
player_node:Yaw(dmouse.x * 0.1)
|
2014-10-15 04:14:20 +03:00
|
|
|
--[[log:info("y="..player_node:GetRotation():YawAngle())
|
|
|
|
log:info("p="..camera_node:GetRotation():PitchAngle())]]
|
2014-10-13 01:07:09 +03:00
|
|
|
|
2014-10-13 01:32:58 +03:00
|
|
|
local body = player_node:GetComponent("RigidBody")
|
|
|
|
|
|
|
|
local wanted_v = magic.Vector3(0, 0, 0)
|
|
|
|
|
2014-10-13 01:07:09 +03:00
|
|
|
if magic.input:GetKeyDown(magic.KEY_W) then
|
2014-10-13 01:32:58 +03:00
|
|
|
wanted_v.x = wanted_v.x + 1
|
2014-10-13 01:07:09 +03:00
|
|
|
end
|
|
|
|
if magic.input:GetKeyDown(magic.KEY_S) then
|
2014-10-13 01:32:58 +03:00
|
|
|
wanted_v.x = wanted_v.x - 1
|
2014-10-13 01:07:09 +03:00
|
|
|
end
|
|
|
|
if magic.input:GetKeyDown(magic.KEY_D) then
|
2014-10-13 01:32:58 +03:00
|
|
|
wanted_v.z = wanted_v.z - 1
|
2014-10-13 01:07:09 +03:00
|
|
|
end
|
2014-10-13 01:32:58 +03:00
|
|
|
if magic.input:GetKeyDown(magic.KEY_A) then
|
|
|
|
wanted_v.z = wanted_v.z + 1
|
|
|
|
end
|
|
|
|
|
2014-10-15 11:57:24 +03:00
|
|
|
if player_crouched then
|
|
|
|
wanted_v = wanted_v:Normalized() * MOVE_SPEED / 2
|
|
|
|
else
|
|
|
|
wanted_v = wanted_v:Normalized() * MOVE_SPEED
|
|
|
|
end
|
2014-10-13 01:32:58 +03:00
|
|
|
|
2014-10-13 16:53:44 +03:00
|
|
|
if magic.input:GetKeyDown(magic.KEY_SPACE) or
|
|
|
|
magic.input:GetKeyPress(magic.KEY_SPACE) then
|
|
|
|
if player_touches_ground and
|
|
|
|
math.abs(body.linearVelocity.y) < JUMP_SPEED then
|
2014-10-13 01:32:58 +03:00
|
|
|
wanted_v.y = wanted_v.y + JUMP_SPEED
|
2014-10-13 16:53:44 +03:00
|
|
|
end
|
2014-10-13 01:07:09 +03:00
|
|
|
end
|
|
|
|
if magic.input:GetKeyDown(magic.KEY_SHIFT) then
|
2014-10-15 11:57:24 +03:00
|
|
|
--wanted_v.y = wanted_v.y - MOVE_SPEED
|
2014-10-13 22:53:59 +03:00
|
|
|
|
|
|
|
-- Delay setting this to here so that it's possible to wait for the
|
|
|
|
-- world to load first
|
|
|
|
if body.mass == 0 then
|
|
|
|
body.mass = 70.0
|
|
|
|
end
|
2014-10-15 11:57:24 +03:00
|
|
|
|
|
|
|
if not player_crouched then
|
2014-10-19 23:39:35 +03:00
|
|
|
player_shape:SetCapsule(PLAYER_WIDTH, PLAYER_HEIGHT/2)
|
2014-10-15 11:57:24 +03:00
|
|
|
camera_node.position = magic.Vector3(0, 0.411*PLAYER_HEIGHT/2, 0)
|
|
|
|
player_crouched = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if player_crouched then
|
2014-10-19 23:39:35 +03:00
|
|
|
player_shape:SetCapsule(PLAYER_WIDTH, PLAYER_HEIGHT)
|
2014-10-15 11:57:24 +03:00
|
|
|
player_node:Translate(magic.Vector3(0, PLAYER_HEIGHT/4, 0))
|
|
|
|
camera_node.position = magic.Vector3(0, 0.411*PLAYER_HEIGHT, 0)
|
|
|
|
player_crouched = false
|
|
|
|
end
|
2014-10-13 01:32:58 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local u = player_node.direction
|
|
|
|
local v = u:CrossProduct(magic.Vector3(0, 1, 0))
|
|
|
|
local bv = body.linearVelocity
|
|
|
|
bv.x = 0
|
|
|
|
bv.z = 0
|
|
|
|
if wanted_v.y ~= 0 then
|
|
|
|
bv.y = 0
|
|
|
|
end
|
|
|
|
bv = bv + u * wanted_v.x
|
|
|
|
bv = bv + v * wanted_v.z
|
|
|
|
bv = bv + magic.Vector3(0, 1, 0) * wanted_v.y
|
|
|
|
body.linearVelocity = bv
|
|
|
|
|
|
|
|
local p = player_node:GetWorldPosition()
|
|
|
|
misc_text:SetText("("..math.floor(p.x + 0.5)..", "..
|
|
|
|
math.floor(p.y + 0.5)..", "..math.floor(p.z + 0.5)..")")
|
|
|
|
end
|
|
|
|
|
|
|
|
player_touches_ground = false
|
|
|
|
end)
|
|
|
|
|
|
|
|
magic.SubscribeToEvent("PhysicsCollision", function(event_type, event_data)
|
|
|
|
--log:info("PhysicsCollision")
|
|
|
|
local node_a = event_data:GetPtr("Node", "NodeA")
|
|
|
|
local node_b = event_data:GetPtr("Node", "NodeB")
|
|
|
|
local contacts = event_data:GetBuffer("Contacts")
|
|
|
|
if node_a:GetID() == player_node:GetID() or
|
|
|
|
node_b:GetID() == player_node:GetID() then
|
|
|
|
while not contacts.eof do
|
|
|
|
local position = contacts:ReadVector3()
|
|
|
|
local normal = contacts:ReadVector3()
|
|
|
|
local distance = contacts:ReadFloat()
|
|
|
|
local impulse = contacts:ReadFloat()
|
|
|
|
--log:info("normal: ("..normal.x..", "..normal.y..", "..normal.z..")")
|
2014-10-13 16:53:44 +03:00
|
|
|
if normal.y < 0.5 then
|
2014-10-13 01:32:58 +03:00
|
|
|
player_touches_ground = true
|
|
|
|
end
|
2014-10-13 01:07:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
function setup_simple_voxel_data(node)
|
2014-10-19 18:46:42 +03:00
|
|
|
local voxel_reg = voxelworld.get_voxel_registry()
|
|
|
|
local atlas_reg = voxelworld.get_atlas_registry()
|
|
|
|
|
2014-10-13 01:07:09 +03:00
|
|
|
local data = node:GetVar("simple_voxel_data"):GetBuffer()
|
|
|
|
local w = node:GetVar("simple_voxel_w"):GetInt()
|
|
|
|
local h = node:GetVar("simple_voxel_h"):GetInt()
|
|
|
|
local d = node:GetVar("simple_voxel_d"):GetInt()
|
|
|
|
log:info(dump(node:GetName()).." voxel data size: "..data:GetSize())
|
2014-10-19 14:18:39 +03:00
|
|
|
buildat.set_8bit_voxel_geometry(node, w, h, d, data, voxel_reg, atlas_reg)
|
2014-10-13 01:07:09 +03:00
|
|
|
node:SetScale(magic.Vector3(1, 1, 1))
|
2014-10-12 10:06:07 +03:00
|
|
|
end
|
2014-10-13 01:07:09 +03:00
|
|
|
|
2014-10-19 19:59:04 +03:00
|
|
|
voxelworld.sub_ready(function()
|
|
|
|
-- Subscribe to this only after the voxelworld is ready because we are using
|
|
|
|
-- voxelworld's registries
|
|
|
|
replicate.sub_sync_node_added({}, function(node)
|
|
|
|
if not node:GetVar("simple_voxel_data"):IsEmpty() then
|
|
|
|
setup_simple_voxel_data(node)
|
|
|
|
end
|
|
|
|
local name = node:GetName()
|
|
|
|
end)
|
2014-10-13 01:07:09 +03:00
|
|
|
end)
|
2014-10-12 10:06:07 +03:00
|
|
|
|
|
|
|
-- vim: set noet ts=4 sw=4:
|