YouGroove Posted August 11, 2013 Share Posted August 11, 2013 In fact when LE3 started, seeing all Player.lua script, oh god, how awfull to have so much code doing so much stuff in one file It's lot more hard to understand and use , or make evolve indeed. So i give to community some litlle example i'm working on. States.lua that has two main functions : -UpdateStates() -UpdateAnimations() I could had made two lua files , but it's really clear in one file as anims and states uses two functions for each purpose. -- states pState={} pState.idle=2 pState.walk=1 pState.run=4 pState.attack=5 pState.hurt=4 pState.dead=0 --Animations pSequence={} pSequence.walk=1 pSequence.run=4 pSequence.idle=2 pSequence.attack0=5 pSequence.attack1=6 pSequence.hit = 5 pSequence.death = 0 -------------------------------------------------- --Normalized vector direction player movin in X,Z -------------------------------------------------- pState.movement=nil pState.playerMoving=false pState.attackInProgress=false ---------- Player States pState.currentState=2 pState.prevState=2 ------------------------------------------------------------------------------------------- -- States function ------------------------------------------------------------------------------------------- function UpdateStates(keyAttack,keyRun,Rkey,Lkey,Ukey,Dkey) ------------ Normalized vector movement for walk and run ----------- pState.movement=Vec3() pState.move=0 pState.prevState = pState.currentState -------------idle state if pState.currentState~=pState.attack then pState.currentState=pState.idle end ------------attack state if pState.currentState~=pState.attack and keyAttack then pState.currentState=pState.attack pState.attackInProgress = true end ----------- move states pState.playerMoving = false if Rkey == true then pState.movement.x=pState.movement.x+1 pState.playerMoving = true end if Lkey == true then pState.movement.x=pState.movement.x-1 pState.playerMoving = true end if Ukey == true then pState.movement.z=pState.movement.z+1 pState.playerMoving = true end if Dkey == true then pState.movement.z=pState.movement.z-1 pState.playerMoving = true end ------------walk or run states if pState.playerMoving ==true and pState.currentState~=pState.attack then if keyRun == true then pState.currentState=pState.run else pState.currentState=pState.walk end end end -------------------------------------------------------------------------------------- -- Animations function --------------------------------------------------------------------------------------- function UpdateAnimations(animationmanager) if pState.prevState ~= pState.currentState then if animationmanager then if pState.currentState==pState.idle then animationmanager:SetAnimationSequence(pSequence.idle,0.005,200) elseif pState.currentState==pState.walk then animationmanager:SetAnimationSequence(pSequence.walk,0.05,200) elseif pState.currentState==pState.run then animationmanager:SetAnimationSequence(pSequence.run,0.08,200) elseif pState.currentState==pState.attack then animationmanager:SetAnimationSequence(pSequence.attack0,0.03,200,1,self,pEndAttack) --LaunchFireBall(self.entity) end end end end function pEndAttack() pState.currentState=pState.idle end It's lot more clear as states function, only deals with states (and some vector movement only) , it don't deals with gameplay ro animations., you just care about states management. That's same thing for animation function as it just calls the right animation based on actual states nothing else. And here is the simple code for UpdatePhysics() in the player code that uses States.lua : --------------------------------------------- -- PLAYER UPDATE PHYSICS --------------------------------------------- ... ... local window=Window:GetCurrent() local move=0 -------------- Player inputs read --------------------- local keyAttack= window:KeyDown(Key.Space) local keyRun = window:KeyDown(Key.Shift) local Rkey = window:KeyDown(Key.Right) local Lkey = window:KeyDown(Key.Left) local Ukey = window:KeyDown(Key.Up) local Dkey = window:KeyDown(Key.Down) ------------------ States.lua function --------------------------- UpdateStates(keyAttack,keyRun,Rkey,Lkey,Ukey,Dkey) ------------------ Move Player -------------------- if pState.currentState == pState.walk then move=self.MoveSpeed end if pState.currentState == pState.run then move=self.RunSpeed end if pState.playerMoving==true then local targetRotation = Math:ATan2(pState.movement.x,pState.movement.z)-180 self.currentyrotation = Math:IncAngle(targetRotation,self.currentyrotation,self.turnSpeed) end self.entity:SetInput(self.currentyrotation,move,0,0,false,self.maxAcceleration) ----------------- States.lua function ------------------- UpdateAnimations(self.animationmanager) As you can see, once again this reamins super clear, and you can concentrate on gameplay code in player.lua instead of loosing yourself in states and animations problems and erros. I just can't no more use or read Darkness code You can adapt this code to other characters indeed, you just need to define the good sequences and states in a copy of that example, than just call them in the character main Lua file like goblin.lua for example. Quote Stop toying and make games Link to comment Share on other sites More sharing options...
YouGroove Posted August 11, 2013 Author Share Posted August 11, 2013 I'll make that as a contribution it will be splitted in two Lua files : -states.lua -animations.lua And i'll give some basic use example with some short and quick player script. Quote Stop toying and make games Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.