Style fixes/improvements

This commit is contained in:
octacian 2017-02-07 14:07:18 -08:00
parent 5902b1aa2f
commit 39751eb34e

View File

@ -29,7 +29,7 @@ Usage: /camera
local recordings = {} local recordings = {}
-- Load recordings -- [function] Load recordings
local path = minetest.get_worldpath() local path = minetest.get_worldpath()
local function load() local function load()
@ -42,13 +42,15 @@ local function load()
end end
end end
-- Call load
load() load()
-- [function] Save recordings
function save() function save()
io.open(path.."/recordings.txt", "w"):write(minetest.serialize(recordings)) io.open(path.."/recordings.txt", "w"):write(minetest.serialize(recordings))
end end
-- [function] Get recording list for chat -- [function] Get recording list per-player for chat
function get_recordings(name) function get_recordings(name)
local recs = recordings[name] local recs = recordings[name]
local list = "" local list = ""
@ -63,13 +65,13 @@ function get_recordings(name)
end end
end end
-- Register on shutdown -- [event] On shutdown save recordings
minetest.register_on_shutdown(save) minetest.register_on_shutdown(save)
-- Table for storing unsaved temporary recordings -- Table for storing unsaved temporary recordings
local temp = {} local temp = {}
-- camera def -- Camera definition
local camera = { local camera = {
description = "Camera", description = "Camera",
visual = "wielditem", visual = "wielditem",
@ -95,18 +97,21 @@ local camera = {
} }
-- on step -- [event] On step
function camera:on_step(dtime) function camera:on_step(dtime)
-- if not driver, remove object
if not self.driver then if not self.driver then
self.object:remove() self.object:remove()
return return
end end
local pos = self.object:getpos() local pos = self.object:getpos()
local vel = self.object:getvelocity() local vel = self.object:getvelocity()
local dir = self.driver:get_look_dir() local dir = self.driver:get_look_dir()
-- if record mode
if self.mode == 0 then if self.mode == 0 then
-- record -- Update path
self.path[#self.path + 1] = { self.path[#self.path + 1] = {
pos = pos, pos = pos,
velocity = vel, velocity = vel,
@ -114,67 +119,78 @@ function camera:on_step(dtime)
yaw = self.driver:get_look_yaw() yaw = self.driver:get_look_yaw()
} }
-- player control of vehicle -- Modify yaw and pitch to match driver (player)
-- always modify yaw/pitch to match player
self.object:set_look_pitch(self.driver:get_look_pitch()) self.object:set_look_pitch(self.driver:get_look_pitch())
self.object:set_look_yaw(self.driver:get_look_yaw()) self.object:set_look_yaw(self.driver:get_look_yaw())
-- accel/decel/stop -- Get controls
local ctrl = self.driver:get_player_control() local ctrl = self.driver:get_player_control()
-- Initialize speed
local speed = vector.distance(vector.new(), vel) local speed = vector.distance(vector.new(), vel)
-- if up, accelerate forward
if ctrl.up then if ctrl.up then
-- forward accelerate
speed = math.min(speed + 0.1, 20) speed = math.min(speed + 0.1, 20)
end end
-- if down, accelerate backward
if ctrl.down then if ctrl.down then
-- backward accelerate
speed = math.max(speed - 0.1, -20) speed = math.max(speed - 0.1, -20)
end end
-- if jump, brake
if ctrl.jump then if ctrl.jump then
-- brake
speed = math.max(speed * 0.9, 0.0) speed = math.max(speed * 0.9, 0.0)
end end
-- if sneak, stop recording
if ctrl.sneak then if ctrl.sneak then
-- stop recording!
self.driver:set_detach() self.driver:set_detach()
minetest.chat_send_player(self.driver:get_player_name(), "Recorded stopped after " .. #self.path .. " points") minetest.chat_send_player(self.driver:get_player_name(), "Recorded stopped after " .. #self.path .. " points")
temp[self.driver:get_player_name()] = table.copy(self.path) temp[self.driver:get_player_name()] = table.copy(self.path)
self.object:remove() self.object:remove()
return return
end end
-- Set updated velocity
self.object:setvelocity(vector.multiply(self.driver:get_look_dir(), speed)) self.object:setvelocity(vector.multiply(self.driver:get_look_dir(), speed))
elseif self.mode == 1 then elseif self.mode == 1 then -- elseif playback mode
-- stop playback ? -- Get controls
local ctrl = self.driver:get_player_control() local ctrl = self.driver:get_player_control()
-- if sneak or no path, stop playback
if ctrl.sneak or #self.path < 1 then if ctrl.sneak or #self.path < 1 then
-- stop playback
self.driver:set_detach() self.driver:set_detach()
minetest.chat_send_player(self.driver:get_player_name(), "Playback stopped") minetest.chat_send_player(self.driver:get_player_name(), "Playback stopped")
self.object:remove() self.object:remove()
return return
end end
-- playback -- Update position
self.object:moveto(self.path[1].pos, true) self.object:moveto(self.path[1].pos, true)
-- Update yaw/pitch
self.driver:set_look_yaw(self.path[1].yaw - (math.pi/2)) self.driver:set_look_yaw(self.path[1].yaw - (math.pi/2))
self.driver:set_look_pitch(0 - self.path[1].pitch) self.driver:set_look_pitch(0 - self.path[1].pitch)
-- Update velocity
self.object:setvelocity(self.path[1].velocity) self.object:setvelocity(self.path[1].velocity)
-- Remove path table
table.remove(self.path, 1) table.remove(self.path, 1)
end end
end end
-- Register entity. -- Register entity
minetest.register_entity("camera:camera", camera) minetest.register_entity("camera:camera", camera)
-- Register chatcommand. -- Register chatcommand
minetest.register_chatcommand("camera", { minetest.register_chatcommand("camera", {
description = "Manipulate recording", description = "Manipulate recording",
params = "<option>", params = "<option> <value>",
func = function(name, param) func = function(name, param)
local player = minetest.get_player_by_name(name) -- Get player name local player = minetest.get_player_by_name(name)
local param1 = param:split(" ")[1] local param1, param2 = param:split(" ")[1], param:split(" ")[2]
local param2 = param:split(" ")[2]
-- if play, begin playback preperation
if param1 == "play" then if param1 == "play" then
local function play(path) local function play(path)
local object = minetest.add_entity(player:getpos(), "camera:camera") local object = minetest.add_entity(player:getpos(), "camera:camera")
@ -186,12 +202,13 @@ minetest.register_chatcommand("camera", {
-- Check for param2 (recording name) -- Check for param2 (recording name)
if param2 and param2 ~= "" then if param2 and param2 ~= "" then
-- if recording exists, start
if recordings[name][param2] then if recordings[name][param2] then
play(table.copy(recordings[name][param2])) play(table.copy(recordings[name][param2]))
else else -- else, return error
return false, "Invalid recording "..param2..". Use /camera list to list recordings." return false, "Invalid recording "..param2..". Use /camera list to list recordings."
end end
else -- else, Check temp else -- else, check temp for a recording path
if temp[name] then if temp[name] then
play(table.copy(temp[name])) play(table.copy(temp[name]))
else else
@ -200,20 +217,22 @@ minetest.register_chatcommand("camera", {
end end
return true, "Playback started" return true, "Playback started"
elseif param1 == "save" then elseif param1 == "save" then -- elseif save, prepare to save path
-- if no table for player in recordings, initialize
if not recordings[name] then if not recordings[name] then
recordings[name] = {} recordings[name] = {}
end end
-- if param2 is not blank, save
if param2 and param2 ~= "" then if param2 and param2 ~= "" then
recordings[name][param2] = temp[name] recordings[name][param2] = temp[name]
return true, "Saved recording as "..param2 return true, "Saved recording as "..param2
else else -- else, return error
return false, "Missing name to save recording under (/camera save <name>)" return false, "Missing name to save recording under (/camera save <name>)"
end end
elseif param1 == "list" then elseif param1 == "list" then -- elseif list, list recordings
return true, "Recordings: "..get_recordings(name) return true, "Recordings: "..get_recordings(name)
else else -- else, begin recording
local object = minetest.add_entity(player:getpos(), "camera:camera") local object = minetest.add_entity(player:getpos(), "camera:camera")
object:get_luaentity():init(player, 0) object:get_luaentity():init(player, 0)
object:setyaw(player:get_look_yaw()) object:setyaw(player:get_look_yaw())
@ -222,8 +241,3 @@ minetest.register_chatcommand("camera", {
end end
end, end,
}) })
-- FIXME
-- add permanent recording of a path
-- add autoplayback on start for singleplayer if autosave path exists.
-- add loop playback