Pārlūkot izejas kodu

samples: cleanup

Daniele Bartolini 7 gadi atpakaļ
vecāks
revīzija
bce13acf15

+ 1 - 1
samples/core/editors/level_editor/boot.package

@@ -1,8 +1,8 @@
 lua = [
 	"core/editors/level_editor/boot"
 	"core/editors/level_editor/camera"
-	"core/editors/level_editor/class"
 	"core/editors/level_editor/level_editor"
+	"core/lua/class"
 ]
 
 physics_config = [

+ 13 - 13
samples/core/editors/level_editor/camera.lua

@@ -1,8 +1,8 @@
-require "core/editors/level_editor/class"
+require "core/lua/class"
 
-FPSCamera = class(FPSCamera)
+Camera = class(Camera)
 
-function FPSCamera:init(world, unit)
+function Camera:init(world, unit)
 	self._world = world
 	self._unit = unit
 	self._translation_speed = 20
@@ -10,28 +10,28 @@ function FPSCamera:init(world, unit)
 	self._perspective_local_pose = Matrix4x4Box(Matrix4x4.identity())
 end
 
-function FPSCamera:unit()
+function Camera:unit()
 	return self._unit;
 end
 
-function FPSCamera:camera()
+function Camera:camera()
 	return World.camera_instances(self._world, self._unit)
 end
 
-function FPSCamera:local_pose()
+function Camera:local_pose()
 	local sg = World.scene_graph(self._world)
 	return SceneGraph.local_pose(sg, self._unit)
 end
 
-function FPSCamera:is_orthographic()
+function Camera:is_orthographic()
 	return World.camera_projection_type(self._world, self._unit) == "orthographic"
 end
 
-function FPSCamera:mouse_wheel(delta)
+function Camera:mouse_wheel(delta)
 	self._translation_speed = math.max(0.001, self._translation_speed + delta * 0.2)
 end
 
-function FPSCamera:camera_ray(x, y)
+function Camera:camera_ray(x, y)
 	local near = World.camera_screen_to_world(self._world, self._unit, Vector3(x, y, 0))
 	local far = World.camera_screen_to_world(self._world, self._unit, Vector3(x, y, 1))
 	local dir = World.camera_projection_type(self._world, self._unit) == "orthographic"
@@ -40,7 +40,7 @@ function FPSCamera:camera_ray(x, y)
 	return near, dir
 end
 
-function FPSCamera:set_perspective()
+function Camera:set_perspective()
 	if not self:is_orthographic() then
 		return
 	end
@@ -50,7 +50,7 @@ function FPSCamera:set_perspective()
 	World.camera_set_projection_type(self._world, self._unit, "perspective")
 end
 
-function FPSCamera:set_orthographic(world_center, world_radius, dir, up)
+function Camera:set_orthographic(world_center, world_radius, dir, up)
 	if not self:is_orthographic() then
 		self._perspective_local_pose:store(self:local_pose())
 	end
@@ -62,14 +62,14 @@ function FPSCamera:set_orthographic(world_center, world_radius, dir, up)
 	World.camera_set_projection_type(self._world, self._unit, "orthographic")
 end
 
-function FPSCamera:screen_length_to_world_length(position, length)
+function Camera:screen_length_to_world_length(position, length)
 	local right = Matrix4x4.x(self:local_pose())
 	local a = World.camera_world_to_screen(self._world, self._unit, position)
 	local b = World.camera_world_to_screen(self._world, self._unit, position + right)
 	return length / Vector3.distance(a, b)
 end
 
-function FPSCamera:update(dt, dx, dy, keyboard, mouse)
+function Camera:update(dt, dx, dy, keyboard, mouse)
 	local sg = World.scene_graph(self._world)
 
 	local camera_local_pose = SceneGraph.local_pose(sg, self._unit)

+ 0 - 23
samples/core/editors/level_editor/class.lua

@@ -1,23 +0,0 @@
-function class(klass, super)
-	if not klass then
-		klass = {}
-		
-		local meta = {}
-		meta.__call = function(self, ...)
-			local object = {}
-			setmetatable(object, klass)
-			if object.init then object:init(...) end
-			return object
-		end
-		setmetatable(klass, meta)
-	end
-	
-	if super then
-		for k,v in pairs(super) do
-			klass[k] = v
-		end
-	end
-	klass.__index = klass
-	
-	return klass
-end

+ 17 - 17
samples/core/editors/level_editor/level_editor.lua

@@ -1,5 +1,5 @@
 require "core/editors/level_editor/camera"
-require "core/editors/level_editor/class"
+require "core/lua/class"
 
 Colors = {
 	grid          = function() return Color4(102, 102, 102, 255) end,
@@ -1313,7 +1313,7 @@ function LevelEditor:init()
 	self._sg = World.scene_graph(self._world)
 	self._lines_no_depth = World.create_debug_line(self._world, false)
 	self._lines = World.create_debug_line(self._world, true)
-	self._fpscamera = FPSCamera(self._world, World.spawn_unit(self._world, "core/units/camera"))
+	self._camera = Camera(self._world, World.spawn_unit(self._world, "core/units/camera"))
 	self._mouse = { x = 0, y = 0, x_last = 0, y_last = 0, button = { left = false, middle = false, right = false }, wheel = { delta = 0 }}
 	self._keyboard = { ctrl = false, shift = false, alt = false }
 	self._grid = { size = 1 }
@@ -1336,8 +1336,8 @@ function LevelEditor:init()
 	-- Spawn camera
 	local pos = Vector3(20, 20, -20)
 	local dir = Vector3.normalize(Vector3.zero() - pos)
-	SceneGraph.set_local_rotation(self._sg, self._fpscamera:unit(), Quaternion.look(dir))
-	SceneGraph.set_local_position(self._sg, self._fpscamera:unit(), pos)
+	SceneGraph.set_local_rotation(self._sg, self._camera:unit(), Quaternion.look(dir))
+	SceneGraph.set_local_position(self._sg, self._camera:unit(), pos)
 end
 
 function LevelEditor:update(dt)
@@ -1355,8 +1355,8 @@ function LevelEditor:update(dt)
 	self._mouse.x_last = self._mouse.x
 	self._mouse.y_last = self._mouse.y
 
-	self._fpscamera:mouse_wheel(self._mouse.wheel.delta)
-	self._fpscamera:update(dt, self._mouse.dx, self._mouse.dy, self._keyboard, self._mouse)
+	self._camera:mouse_wheel(self._mouse.wheel.delta)
+	self._camera:update(dt, self._mouse.dx, self._mouse.dy, self._keyboard, self._mouse)
 
 	self.tool:update(dt, self._mouse.x, self._mouse.y)
 
@@ -1364,7 +1364,7 @@ function LevelEditor:update(dt)
 	self._mouse.dy = 0
 	self._mouse.wheel.delta = 0
 
-	local pos, dir = self._fpscamera:camera_ray(self._mouse.x, self._mouse.y)
+	local pos, dir = self._camera:camera_ray(self._mouse.x, self._mouse.y)
 	local selected_object, t = raycast(self._objects, pos, dir)
 	self._spawn_height = selected_object and (pos + dir * t).y or 0
 
@@ -1378,7 +1378,7 @@ function LevelEditor:update(dt)
 end
 
 function LevelEditor:render(dt)
-	Device.render(self._world, self._fpscamera:unit())
+	Device.render(self._world, self._camera:unit())
 end
 
 function LevelEditor:shutdown()
@@ -1440,7 +1440,7 @@ function LevelEditor:key_up(key)
 end
 
 function LevelEditor:camera()
-	return self._fpscamera
+	return self._camera
 end
 
 function LevelEditor:set_tool(tool)
@@ -1512,7 +1512,7 @@ function LevelEditor:snap(grid_tm, pos)
 end
 
 function LevelEditor:find_spawn_point(x, y)
-	local pos, dir = self._fpscamera:camera_ray(x, y)
+	local pos, dir = self._camera:camera_ray(x, y)
 	local spawn_height = LevelEditor._spawn_height
 	local point = Vector3(0, spawn_height, 0)
 	local normal = Vector3.up()
@@ -1594,29 +1594,29 @@ function LevelEditor:destroy(id)
 end
 
 function LevelEditor:camera_view_perspective()
-	self._fpscamera:set_perspective()
+	self._camera:set_perspective()
 end
 
 function LevelEditor:camera_view_front()
-	self._fpscamera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(0, 0, 1), Vector3(0, 1, 0))
+	self._camera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(0, 0, 1), Vector3(0, 1, 0))
 end
 
 function LevelEditor:camera_view_back()
-	self._fpscamera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(0, 0, -1), Vector3(0, 1, 0))
+	self._camera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(0, 0, -1), Vector3(0, 1, 0))
 end
 
 function LevelEditor:camera_view_right()
-	self._fpscamera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(-1, 0, 0), Vector3(0, 1, 0))
+	self._camera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(-1, 0, 0), Vector3(0, 1, 0))
 end
 
 function LevelEditor:camera_view_left()
-	self._fpscamera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(1, 0, 0), Vector3(0, 1, 0))
+	self._camera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(1, 0, 0), Vector3(0, 1, 0))
 end
 
 function LevelEditor:camera_view_top()
-	self._fpscamera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(0, -1, 0), Vector3(0, 0, 1))
+	self._camera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(0, -1, 0), Vector3(0, 0, 1))
 end
 
 function LevelEditor:camera_view_bottom()
-	self._fpscamera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(0, 1, 0), Vector3(0, 0, 1))
+	self._camera:set_orthographic(Vector3(0, 0, 0), Vector3(1, 1, 1), Vector3(0, 1, 0), Vector3(0, 0, 1))
 end

+ 1 - 1
samples/core/editors/unit_preview/boot.package

@@ -1,8 +1,8 @@
 lua = [
 	"core/editors/level_editor/camera"
-	"core/editors/level_editor/class"
 	"core/editors/unit_preview/boot"
 	"core/editors/unit_preview/unit_preview"
+	"core/lua/class"
 ]
 
 physics_config = [

+ 4 - 4
samples/core/editors/unit_preview/unit_preview.lua

@@ -7,7 +7,7 @@ function UnitPreview:init()
 	self._sg = World.scene_graph(self._world)
 	self._rw = World.render_world(self._world)
 	self._pw = World.physics_world(self._world)
-	self._fpscamera = FPSCamera(self._world, World.spawn_unit(self._world, "core/units/camera"))
+	self._camera = Camera(self._world, World.spawn_unit(self._world, "core/units/camera"))
 	self._unit_name = nil
 	self._unit_id = nil
 
@@ -28,7 +28,7 @@ function UnitPreview:update(dt)
 		radius = radius <   1 and   1 or radius
 		radius = radius > 100 and 100 or radius
 
-		local camera_unit = self._fpscamera:unit()
+		local camera_unit = self._camera:unit()
 		local pos = Vector3(radius, radius, -radius) * 2
 		local camera_pos = Matrix4x4.translation(tm) + pos
 		local target_pos = Matrix4x4.translation(tm)
@@ -36,11 +36,11 @@ function UnitPreview:update(dt)
 		SceneGraph.set_local_position(self._sg, camera_unit, camera_pos)
 	end
 
-	self._fpscamera:update(dt, 0, 0, {})
+	self._camera:update(dt, 0, 0, {})
 end
 
 function UnitPreview:render(dt)
-	Device.render(self._world, self._fpscamera:unit())
+	Device.render(self._world, self._camera:unit())
 end
 
 function UnitPreview:shutdown()