RoboCatz.com
About FLL | Navigation | Playbook | Strategy | Models | Techniques | RobotC | Programming | Robot News |

--Art Installation Script for Roblox

--Art Installation Script for Roblox
--[[
Updated: 5/19/2023

This module is called: artInstallationRoom and is stored in ReplicatedStorage

This module will create a space for a virtual art exhibit to be used with genericSpapes

]]

local artRoom = {}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local s = require(ReplicatedStorage.genericShapes)

local defaultRoomWidth, defaultRoomLength = 75, 75

-- Get and set the default room width and length
function artRoom.setWidth(w) defaultRoomWidth = w end
function artRoom.setLength(l) defaultRoomLength = l end
function artRoom.getWidth() return defaultRoomWidth end
function artRoom.getLength() return defaultRoomLength end

function artRoom.new(arguments)
	local roomStruct = { 
		width=defaultRoomWidth, 
		length=defaultRoomLength, 
		height=40,
		size=nil,
		position = nil,
		roomVolume = nil,
		movedByVector = nil,                              -- Used to keep track of where the room has been shifted to.  Updated in the .reposition() function.
		region = nil,
		floorMaterial = Enum.Material.Marble,
		floorColor = Color3.new(1, 1, 1),
		floorPart = nil,
		floorCenter = nil,
		ceilingMaterial = Enum.Material.Plastic,
		ceilingColor = Color3.new(0, 0, 0),
		ceilingOpacity = 1,  -- was 1
		ceilingLights = {},
		ceilingLightLevel = 400,  -- was 400
		ceilingPart = nil,
		wallMaterial = Enum.Material.Plastic,
		wallColor = Color3.new(1, 1, 1),
		isInTheRoom = false,
		center = nil
	}
	-- Examine for any passed parameters to change the room characteristics
	-- print("creating new room")
	-- Render the room now
	roomStruct.size = Vector3.new(roomStruct.width, roomStruct.height, roomStruct.length)
	-- Floor
	local x = 0 -- This will center the floor panel on x=0
	local z = -1*(roomStruct.length/2)-10
	roomStruct.floorPart = s.panel({x=x, y=0, z=z, width=roomStruct.width, length=1, height=roomStruct.length, material=roomStruct.floorMaterial, color=roomStruct.floorColor, rotateBy=Vector3.new(0, 90, 0)})
	roomStruct.floorCenter = Vector3.new(x, 0, z)
	roomStruct.center = Vector3.new(x, roomStruct.height/2, z)
	-- print(typeof(roomStruct.floorPart.Position))
	-- Walls
	-- wall are not constructed here.  They are constructed by the maze program??
	-- Ceiling
	roomStruct.ceilingPart = s.panel(x, roomStruct.height, -1*(roomStruct.length/2)-10, roomStruct.width, 0.2, roomStruct.length, roomStruct.ceilingMaterial, roomStruct.ceilingColor)
	s.opacity(roomStruct.ceilingPart, roomStruct.ceilingOpacity)
	-- Lighting
	roomStruct.ceilingLights = {
		s.lightSource(x-roomStruct.width/4, roomStruct.height-5, (-1*(roomStruct.length/2))-(roomStruct.length/4)-10, roomStruct.width/12, 0.2, roomStruct.length/12),
		s.lightSource(x-roomStruct.width/4, roomStruct.height-5, (-1*(roomStruct.length/2))+(roomStruct.length/4)-10, roomStruct.width/12, 0.2, roomStruct.length/12),
		s.lightSource(x+roomStruct.width/4, roomStruct.height-5, (-1*(roomStruct.length/2))-(roomStruct.length/4)-10, roomStruct.width/12, 0.2, roomStruct.length/12),
		s.lightSource(x+roomStruct.width/4, roomStruct.height-5, (-1*(roomStruct.length/2))+(roomStruct.length/4)-10, roomStruct.width/12, 0.2, roomStruct.length/12)
	}

	return roomStruct
end

function artRoom.repositionTheArt(exhibitRoom)
	-- print("Moving the art")
	-- Re-position art work
	for i = 1, #exhibitRoom.parts do exhibitRoom.parts[i].Position += exhibitRoom.room.movedByVector end
end

function artRoom.reposition(exhibitRoom, x, z)
	-- print(`Moving the room to: {x}, {z}`)
	exhibitRoom.room.movedByVector = Vector3.new(x-exhibitRoom.room.size.x/2, 0, z-exhibitRoom.room.size.z/2)
	-- Re-position floor and ceiling
	exhibitRoom.room.floorPart.Position += exhibitRoom.room.movedByVector
	exhibitRoom.room.ceilingPart.Position += exhibitRoom.room.movedByVector
	exhibitRoom.room.floorCenter += exhibitRoom.room.movedByVector
	exhibitRoom.room.center += exhibitRoom.room.movedByVector
	for i = 1, #exhibitRoom.room.ceilingLights do
		exhibitRoom.room.ceilingLights[i].Position += exhibitRoom.room.movedByVector
	end
	exhibitRoom.room.position = exhibitRoom.room.movedByVector
	-- Re-position art work
	for i = 1, #exhibitRoom.parts do
		exhibitRoom.parts[i].Position += exhibitRoom.room.movedByVector
	end
	-- print(typeof(exhibitRoom.room.floorPart.Position))
	-- Generate the region
	local newRoomCenter = exhibitRoom.room.floorPart.Position
	exhibitRoom.room.region = Region3.new(newRoomCenter - (exhibitRoom.room.size/2), newRoomCenter + (exhibitRoom.room.size/2)) 
end


function artRoom.destroy(partsList)
	print("Destroying room")
	for i = 1, #partsList do
		partsList[i]:Destroy()
	end
end

function artRoom.isInRoom(vector, region)  -- (human, room)
	-- validate input
	assert(typeof(vector) == "Vector3")
	assert(typeof(region) == "Region3")

	-- get the dimensions of the region
	local regionCenter = region.CFrame.Position
	local regionHalfSize = region.Size * 0.5

	-- calculate the extents of the region
	local lowerLeftCorner = regionCenter - regionHalfSize
	local upperRightCorner = regionCenter + regionHalfSize

	-- check if the vector is inside the extents
	local isBeyondLeftCorner = (vector.X >= lowerLeftCorner.X) and
		(vector.Y >= lowerLeftCorner.Y) and
		(vector.Z >= lowerLeftCorner.Z)
	local isBeforeRightCorner = (vector.X <= upperRightCorner.X) and
		(vector.Y <= upperRightCorner.Y) and
		(vector.Z <= upperRightCorner.Z)

	return isBeyondLeftCorner and isBeforeRightCorner

	-- NOTE : comparing individual values above is less expensive than
	-- return vector.Magnitude >= lowerLeftCorner.Magnitude and
	--     vector.Magnitude <= upperRightCorner.Magnitude
end

return artRoom