示例#1
0
    def CastRayGrapple(self):
        
        #creating a ray
        ray = VectorMath.Ray()
        
        #Testing if grapple has hit anything
        if(self.grappleHit == 0):
            direction = self.grappleDirection
        #if yes setting direction to where grapple is and player
        else:
            direction = self.grapplePoint - VectorMath.Vec3(self.Owner.Transform.Translation.x + (math.cos(self.PointDirection) * .37*self.Owner.Transform.Scale.x), self.Owner.Transform.Translation.y + (math.sin(self.PointDirection) * .4*self.Owner.Transform.Scale.y) + .15, 0) 

        #Calculating vector for grapple
        direction.normalize()
        ray.Direction = direction
        ray.Direction.normalize()
        direction = math.atan2(direction.y, direction.x)
        
        #Setting starting point at player (can adjust if needed)
        ray.Start = VectorMath.Vec3(self.Owner.Transform.Translation.x + (math.cos(self.PointDirection) * .37*self.Owner.Transform.Scale.x), self.Owner.Transform.Translation.y + (math.sin(self.PointDirection) * .4*self.Owner.Transform.Scale.y) + .15, 0) 
        #Increase grapple length if nothing has been hit
        if(self.grappleHit == 0):
            #Grapple speed (can be adjusted if needed)
            self.grappleDistance += self.DeltaTime * 25
        else:
            #distance formula
            self.grappleDistance = math.sqrt(math.pow((ray.Start.x - self.grapplePoint.x), 2) + math.pow((ray.Start.y - self.grapplePoint.y), 2))
            #setting player to kinematic so player can move with grapple
            self.Owner.RigidBody.Kinematic = True
            #stop grapple when the distance between hook and player is 1
            #fixes stuck in the ceiling  bug
            if(self.grappleDistance < 1):
                self.StopGrapple()
                
#----------------------------------------------------------
#Pendulum Swing Related

            #if not swinging at the time
            if(not self.Swing):
                #if mouse is being held down
                if(self.MouseDown == True):
                   #move speed for player to hook on grapple
                   self.Owner.Transform.Translation += ray.Direction * (self.DeltaTime * 18)
                #if mouse is release
                else:
                    #save the current height
                    self.rayY = self.Owner.Transform.Translation.y
                    #begin to swing
                    self.Swing = True
                    #swing right
                    if(ray.Direction.x > 0):
                        self.swingRight = True
                        self.swingDown = True
                        self.swingPlayer(ray.Direction)
                    #swing left
                    else:
                        self.swingRight = False
                        self.swingDown  = True
                        self.swingPlayer(ray.Direction)
            #keep swinging
            else:
                self.swingPlayer(ray.Direction)
                
#----------------------------------------------------------

        #Adds color to the 'Rope'
        rayColor = VectorMath.Vec4(0.45, 0.15, 0, 1)
        #Finds the range of the first thing grapple collides with
        castResultRange = self.Space.PhysicsSpace.CastRayResults(ray, 50)
        
        #checking if the grapple hit anything
        if (self.grappleHit == 0):
            endPosition = ray.Start + ray.Direction * self.grappleDistance
        else:
            #sets position to grapple hit
            endPosition = self.grapplePoint
            
        for castResult in castResultRange:
            #asking if the it hit a certain named object
            #objects with name floor are able to be grappled to
            if(castResult.ObjectHit.Name == "Floor"):
                #attach to this object
                if(castResult.Distance >= self.grappleDistance):
                    break
                #setting endPosition to where it hit
                endPosition = castResult.WorldPosition
                #has it hit anything yet
                if (self.grappleHit == 0):
                    #change it to one if it hit something
                    self.grappleHit = 1
                    #sets grapplePoint to end position so where it hit something
                    self.grapplePoint = endPosition
                    self.Space.SoundSpace.PlayCue("hooked")
                    
            #Prevents grapple from hitting player, key (+ AOE region), gate AOE region, and gold
            elif(castResult.ObjectHit.Name != "Player" and castResult.ObjectHit.Name != "Pit" and castResult.ObjectHit.Name != "Key" and castResult.ObjectHit.Name != "AOE" and castResult.ObjectHit.Name != "GateAOE" and castResult.ObjectHit.Name != "Gold"):
                if(castResult.Distance < self.grappleDistance):
                    self.StopGrapple()
            else:
                pass

        #Scales, Rotates, adds Color, and sets Starting Point of Rope 'Rope'
        self.Grapple.Transform.Scale = Vec3(0.2, (math.sqrt(math.pow((ray.Start.x - endPosition.x), 2) + math.pow((ray.Start.y - endPosition.y), 2)) * 2), 1)
        self.Grapple.Transform.Rotation = VectorMath.Quat(0,0,direction + math.radians(-90))
        self.Grapple.Transform.Translation = ray.Start
        self.Grapple.Sprite.Color = rayColor
        
        #Setting a 'Hook' at end of the grapple
        self.hook.Transform.Translation = ray.Start + ray.Direction * self.grappleDistance
        self.hook.Transform.Rotation = VectorMath.Quat(0,0,direction + math.radians(-90))
        #fixed collisions: whenever grapple is running run this script as well
        self.CastRayStopGrapple()
示例#2
0
    def OnLogicUpdate(self, UpdateEvent):
        
        #don't use the grapple for animated character on main menu
        if(self.Space.CurrentLevel.Name == "MainMenu"):
            self.StopGrapple()
            
        #run the functions for movement. checking for groun and jumping    
        self.ApplyMovement()
        self.UpdateGroundState()
        self.ApplyJumping()
        
        #flip sprite based on the direction it is moving in
        self.Owner.Sprite.FlipX = self.playerDirection

        #if the player has picked up a key place it above players head
        if(self.keyAttached):
            self.Key.Transform.Translation = self.Owner.Transform.Translation + Vec3(0,.6 * self.Owner.Transform.Scale.y,.1)
            
        #if the game is reset destroy HUD and then relaod level
        if(self.ResetIsPressed == True):
            if(not Zero.Game.FindSpaceByName("HUDSpace")):
                pass
            else:
                Zero.Game.FindSpaceByName("HUDSpace").Destroy()
            self.Space.ReloadLevel()
            
#----------------------------------------------------------
#Used for counting gold

        hudSpace = Zero.Game.FindSpaceByName("HUDSpace")
        if(not hudSpace):
            pass
        else:
            goldObject = hudSpace.FindObjectByName("GCounter")
        
        #Update gold/gem counter on the HUD
            if(goldObject):
                goldObject.SpriteText.Text = str(self.gold)

#----------------------------------------------------------
#Grappling Hook Related:
    
        self.DeltaTime = UpdateEvent.Dt
        #loop through grapple if below is True
        if(self.playerGrappleShot):
           self.CastRayGrapple()
           
        #Setting to a vector
        self.MouseDirection = self.mousePosition - self.Owner.Transform.Translation
        self.PointDirection = math.atan2(self.MouseDirection.y, self.MouseDirection.x)
        self.MouseDirection.normalize()
        #Setting arm movement
        if(self.arm):
            self.arm.Transform.Rotation = VectorMath.Quat(0,0, self.PointDirection + math.radians(45))

#----------------------------------------------------------

#----------------------------------------------------------
#Weapon Overheating/Cool down
 
        #If there is heat, start cooling down
        if(self.Heat > 0.0):
            self.Heat -= self.CooldownSpeed * UpdateEvent.Dt
        #if heat reaches Zero, stop cooling and allow shooting if you couldn't shoot
        if(self.Heat < 0.0):
            self.Heat = 0.0
            self.CanShoot = True
        #if heat excedes Overheat threshold, you can't shoot.
        if(self.Heat >= self.Overheat):
            self.Heat = self.Overheat
            self.CanShoot = False