Jump to content

Programming methodology

Yue

80 views

 

When I write programming, that word makes me a lot of weight, I don't really do anything, the engine has everything I need and the only thing that is done is to give instructions that I really think is not programming, because everything is already programmed.

Well, I think I've improved a bit and I share how I'm doing things. There's probably another better way to do this, but I feel comfortable with what I'm doing.

I'm working with object-oriented programming, in this case I create a class called PickObjects. This class contains what is necessary for my character to kick boxes. 


Translated with www.DeepL.com/Translator

 

 

--################################################
--## Proyecto 	: 	Pawn.
--## Sitio Web 	: 	https://www.iris3dgames.xyz
--## Fichero 	: 	PickObjects.lua
--## Scripter   : 	Yue Rexie.
--################################################
--## Notas	:   Fichero Clase PickObjects.
--##			    Interactuar con objetos.
--################################################


PickObjects={}

function PickObjects:New(pivotRay)

	local this={}
	this.ray 	= nil
	this.pivotRay 	= nil
	this.pickInfo 	= nil 
	this.player   	= nil
	this.posPlayer	= Vec3()
	this.posPivotRay= Vec3()
	this.forceKick  = nil
	
	function this:Start(player)
		
		self.pivotRay = pivotRay
		self.player   = player


		self.pivotRay:SetParent(self.player,true )
		


	end

	function this:Update()


		self:RayObject()
		self:KickObject()
		
	end


	function this:RayObject()


		self.posPlayer   = self.player:GetPosition(true)
		self.posPivotRay = self.pivotRay:GetPosition(true)

		self.pickInfo = PickInfo()
	
		self.ray = World:GetCurrent():Pick(self.posPlayer,self.posPivotRay,self.pickInfo,0.1)
		System:Print(self.ray)
	end

	function this:KickObject()

		if self.ray then
			if self.pickInfo.entity:GetKeyValue("name") == "Cajas" then
				if 	Window:GetCurrent():KeyDown(Key.E) then 
				
					self.forceKick  = Vec3(0,1000, 500)
					self.forceKick = Transform:Vector(self.forceKick, self.player, nil)
					self.pickInfo.entity:AddPointForce( self.forceKick,self.player:GetPosition(true))
				end
			end
		end

	end


	return this

end

Already in another file called EPlayer, import this class and create an object. The EPlayer file is the one that is hooked to an entity in the map editor.

 

--################################################
--## Proyecto 	: 	Pawn.
--## Sitio Web 	: 	https://www.iris3dgames.xyz
--## Fichero 	: 	Player.lua
--## Scripter   : 	Yue Rexie.
--################################################
--## Notas	:   Fichero enganchado Al Player.
--##			    Definición del comportamiento.
--################################################


--## Class.
import "Scripts/Player/Class/Player.lua"
import "Scripts/Player/Class/HudPlayer.lua"
import "Scripts/Player/Class/PickObjects.lua"

--## Properties Script.
Script.pivotPlayer  = nil --Entity "Pivot Player"
Script.pivotCamera  = nil --Entity "Pivot Camera"
Script.cameraPlayer = nil --Entity "Camera Player"
Script.pivotRay  = nil --Entity "Pivot Ray"


function Script:Start()
	
	--## Player.
	self.Player = Player:New()
	self.Player:StartCameraOrbit(self.entity,self.pivotPlayer, self.pivotCamera,self.cameraPlayer)

	--## Hud Player.
	self.Hud = HudPlayer:New()
	self.Hud:Start()
		

	--## Pick Objects.
	self.Pick = PickObjects:New(self.pivotRay)
	self.Pick:Start(self.entity )



end



function Script:UpdateWorld()
	
	self.Player:Update()
	self.Pick:Update()


end


--[[
function Script:UpdatePhysics()
	
end
]]

--[[
--This can be used to select which objects an entity collides with.  This overrides collision types completely.
function Script:Overlap(e)
	return Collision:Collide
end
]]


function Script:Collision(entity, position, normal, speed)
	


end



function Script:Draw()
	


end


--[[
function Script:DrawEach(camera)
	
end
]]


function Script:PostRender(context)
	if myMenu.guiHide == 1 then 
		self.Hud:Draw()
	end
end


--[[
--This function will be called when the entity is deleted.
function Script:Detach()
	
end
]]

--[[
--This function will be called when the last instance of this script is deleted.
function Script:Cleanup()
	
end
]]


--This function will be called upon completion of a one-shot animation played with the PlayAnimation() command.
function Script:EndAnimation(sequence)
	self.Player.forceJump = 0

	--## Init Jump.
	if sequence == 2 then 
		
		self.Player.animation = self.Player.idle
		self.typeAnim = 1
		self.Player.blend = 1500
		
		self.Player.velAnim = 0.03
		self.Player.forceJump = 7.0
	end 
	--## Falling.
	if sequence == 1 then
			self.Player.animation = self.Player.idle
			self.typeAnim = 0
			self.Player.blend = 100
	end

	
end

The previous working method helps me to have everything organized, to fragment the program into many smaller pieces and therefore it is more fun to do things.

I'm one of those who likes to start all the scripts and start from scratch, I feel I have more control over the workflow. In that case the Main.lua file is as follows:

 

--################################################
--## Proyecto 	: 	Pawn.
--## Sitio Web 	: 	https://www.iris3dgames.xyz
--## Fichero 	: 	Main.lua
--## Scripter   : 	Yue Rexie.
--################################################
--## Notas	: Fichero principal de entrada
--##			  al programa en Script Lua.
--################################################




--## Class.
import "Scripts/Class/App.lua"
import "Scripts/Class/Menu.lua" 

--## New Object App.
local myApp = App:New("Pawn 0.0 | Alpha")
myApp:InitGraphics(800,600)

--## Load Menu Level 1.
 worldMenu = myApp:CreateWorld()
myApp:LoadMap("menu")

--## Load Level 1.
 worldLevel1 = myApp:CreateWorld()
myApp:LoadMap("start")

--## New Object Menu.
myMenu = Menu:New()
myMenu:CreateGUI()
menuVisible = nil

Texture:SetDetail(0)
local myWorld = nil

--## Check World.
if myMenu.level == 0 then
	myWorld = worldMenu
elseif myMenu.level == 1 then
	myWorld = worldLevel1 
end	
myMenu.guiHide = 1
myApp:SetLightQuality(2)
myApp:FadeIn(myWorld,myMenu)





while myApp:Exit() == false and myMenu:Update(myApp,myWorld,myMenu) == false  do
	menuVisible = myMenu.guiHide -- Script Camera attach.

	--## Check World.
	if myMenu.level == 0 then
		myWorld = worldMenu
		
	elseif myMenu.level == 1 then
		--Window:GetCurrent():HideMouse()
		myWorld = worldLevel1
		worldMenu:Clear()

	end	
	

	
	myApp:UpdateWorld(myWorld)

end
--myApp:FadeOut(myWorld,myMenu)

It is the same principle, two classes are imported in the header, and objects are created on the basis of these classes.

That's all to share, my working methodology, I know it's not perfect and maybe there's another better way to do things. 

  • Like 1


0 Comments


Recommended Comments

There are no comments to display.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...