Weapon Switch

Blitz3D Forums/Blitz3D Programming/Weapon Switch

wizzlefish(Posted 2004) [#1]
How do I make it so you go over to a gun laying on the ground, place your crosshairs on it, and right-click, and it transports to your hand? And then, if the weapon exists in your hand, you can press 1, 2, or 3 to switch weapons. Do you guys have any idea how to do this? I tried, and nothing works...


GfK(Posted 2004) [#2]
I tried, and nothing works...
Linepick works.


wizzlefish(Posted 2004) [#3]
linepick?


Jeremy Alessi(Posted 2004) [#4]
Or CameraPick()

Something like:

If MouseDown(2)
   picked = CameraPick(camera, 320, 240)
endif
If picked = aGun
   oldGun = currentGun
   HideEntity(oldGun)
   currentGun = picked
   EntityParent(currentGun, Hand)
endif



Matty(Posted 2004) [#5]
Or

Have a type that stores
your weapon details such as:
Type Weapon
Field Category ; ie rifle, shotgun, missile launcher, PU36-Space Modulator
Field Index ;ie the slot it takes up in the players' weapon array below
Field X#
Field Y#
Field Z#

Field OffsetX#
Field OffsetY#
Field OffsetZ# ;these are where the gun should be placed relative to the player's space

Field EntHandle ;the model's entity handle
Field OnGround ;True/False
;and any other properties
;you can think of like ammunition etc
end type

;I assume in your player's type you have something similar to this perhaps:

Type Player
field EntHandle ; the player's entity handle
Field X#
Field Y#
field Z#
Field ActiveWeaponIndex
Field weapon[MaxWeaponIndex];nonzero or 0 depending on if the player has this weapon or not
;and whatever other fields you have normally.
end type

;in your input section of the code, when you detect a right mouse click perform the following function:
;




Function PickUpWeapon(CrossHairX,CrossHairY,CameraHandle,P.Player,MaximumPickupDistance#,MaximumMouseDistance)
If P.Player=null then runtimeerror "No player object passed to function"
;we are attempting to pick up a gun...
For Gun.Weapon = each Weapon
IF Gun\EntHandle<>0 and Gun\OnGround=true then
;okay the gun is real and on the ground somewhere or level
GunDist#=Sqr((Gun\X-P\X)^2+(Gun\Y-P\Y)^2+(Gun\Z-P\Z)^2)
CameraProject(CameraHandle,Gun\X,Gun\Y,Gun\Z)
IF (GunDist#)<MaximumPickupDistance and Abs(ProjectedX()-CrossHairX)<MaximumMouseDistance and abs(ProjectedY()-CrossHairY)<MAximumMouseDistance then
;okay the gun is within range and our crosshair is within a certain distance of the gun's position on the screen
;therefore pick the gun up - if we are not already carrying one like this.
If P\Weapon[Gun\Index]=0 then
Gun\OnGround=false
HideEntity Gun\EntHandle
P\Weapon[Gun\Index]=Gun\EntHandle
EntityParent Gun\EntHandle,P\EntHandle
positionentity Gun\EntHandle,P\X,P\Y+G,P\Z
moveentity Gun\EntHandle,G\OffsetX,G\OffsetY,G\OffsetZ

endif
endif
endif
next

end function


Function SwapWeapon(P.Player,WeaponIndex)
;call this function when the player pressed "1" "2" or "3" sending the weapon index (same as gun index)
If P.Player=null then runtimeerror("No player type sent to this function")
IF P\Weapon[WeaponIndex]<>0 then
;ie we are carrying this weapon... so change to it...
For i=0 to MaxWeaponIndex
If P\Weapon[i]<>0 then hideentity P\Weapon[i]
next
ShowEntity P\Weapon[WeaponIndex]
P\ActiveWeaponIndex=WeaponIndex

endif


end function


wizzlefish(Posted 2004) [#6]
OK - I tried that...but for some reason it didn't work. Is there a more simple way? How about this:

You pick up a gun if you run into it. You change guns by pressing one, two, three....how would I do that?

I tried "EntityCollided" but maybe I set it up wrong.


wizzlefish(Posted 2004) [#7]
Why doesn't anyone reply?


poopla(Posted 2004) [#8]
I'd recommend NOT making replies like that Digital. People here are busy! On top of that, the help you recieve is a gift, and you should never act like people helping you is a requirement. If you do that, you'll never get any help.


Jeremy Alessi(Posted 2004) [#9]
Don't use EntityCollided() for something simple like that. Use a distance check ... look at this. I didn't test it or anything just a codesketch but it should work.


;====== TYPES =============================================

Type playerCharacter

     Field playerMesh, guns[5], haveGun[5], currentGun

End Type

Type gunInLevel

     Field gunMesh, gunType

End Type

;==========================================================

;====== SETUP =============================================

For i = 2 to 5

     gIL.gunInLevel = New gunInLevel
     gIL\gunMesh = LoadMesh("Gun" + i + ".b3d")
     gIL\gunType = i
     PositionEntity(gIL\gunMesh, goodPlaceX(), goodPlaceY(), goodPlaceZ())

Next

pC.playerCharacter = New playerCharacter
pC\playerMesh = LoadMesh("Hand.b3d")

pC\currentGun = 1
pC\haveGun[pC\currentGun] = True
For i = 1 to 5

     pC\guns[i] = LoadMesh("Gun" + i + ".b3d")
     PositionEntity(pC\guns[i], LooksGoodX(), LooksGoodY(), LooksGoodZ())
     If i > 1 Then HideEntity(pC\guns[i])

Next

;==========================================================

;====== SOMEWHERE IN YOUR LOGIC ===========================

For gIL.gunInLevel = Each gunInLevel

     If EntityDistance(gIL\gunMesh, pC\mesh) <= 1
          PickUpGun(gIL\gunType, Handle(pC.playerCharacter))
     EndIf

Next

SwitchGun(Handle(pC.playerCharacter))

;==========================================================

;====== GUN FUNCTIONS =====================================

Function PickUpGun(gun, player)

     pC.playerCharacter = Object.playerCharacter(player)
     pC\haveGun[gun] = True
     HideEntity(pC\guns[pC\currentGun])
     pC\currentGun = gun
     ShowEntity(pC\guns[pC\currentGun])

End Function

Function SwitchGun(player)

     pC.playerCharacter = Object.playerCharacter(player)
     If KeyHit(2) and pC\haveGun[1]
          HideEntity(pC\guns[pC\currentGun])
          pC\currentGun = 1
          ShowEntity(pC\guns[pC\currentGun])
     EndIF
     If KeyHit(3) and pC\haveGun[2]
          HideEntity(pC\guns[pC\currentGun])
          pC\currentGun = 2
          ShowEntity(pC\guns[pC\currentGun])
     EndIF
     If KeyHit(4) and pC\haveGun[3]
          HideEntity(pC\guns[pC\currentGun])
          pC\currentGun = 3
          ShowEntity(pC\guns[pC\currentGun])
     EndIF
     If KeyHit(5) and pC\haveGun[4]
          HideEntity(pC\guns[pC\currentGun])
          pC\currentGun = 4
          ShowEntity(pC\guns[pC\currentGun])
     EndIF
     If KeyHit(6) and pC\haveGun[5]
          HideEntity(pC\guns[pC\currentGun])
          pC\currentGun = 5
          ShowEntity(pC\guns[pC\currentGun])
     EndIF

End Function

;==========================================================
     



Bremer(Posted 2004) [#10]
How about showing what you have been trying and perhaps someone could figure out what you might be doing wrong.


Ross C(Posted 2004) [#11]
I'd store everything that is a pickup in a type collection


Type pickup
   field x,y,z
   field p_type ; the type of pickup
   field quantity ; the quantity of the pickup
End Type



Then loop through each type object and do a distance check between the player and each of the type objects. Then, if the obect is within a certain distance, check to see what kind of pickup it is. Then, depending on that, add extra ammo, or health, or add a flag saying you have picked up a gun.

What i'd do, is give you player all the guns he will ever have, but have a flag for each gun, a variable saying gun_available = 0: When switching weapons, check the flag for each gun, If the flag = 1 then, you can switch to that weapon, if not, you don't "have" it.


Jeremy Alessi(Posted 2004) [#12]
Hmmm... that's strangely exactly what I put in code above ;)


jfk EO-11110(Posted 2004) [#13]
It looks like you don't know Blitz good enough. Better learn more before you try to make an FPS. Because if you knew only half of Blitz, you could do this by your own. You won't learn it if other people do it for you.

So hack your way trough this programming language.


wizzlefish(Posted 2004) [#14]
I've already created most of the FPS. This is all I need.


BulletMagnet(Posted 2004) [#15]
Digital,
Think about what you want to do, step by step, in 'pseudo code'. Slowly turn that into working blitz code. If you are stuck, try to break the problem into smaller pieces. A game (or any program) is really tons of small steps that move information around. There is no such thing as a weapon (no spoon Neo), unless you define one. Create all of its properties, and manipulate those to make them behave how you want as the game is processed...

Example:
-You want to check if the player is close to the weapon. Ask yourself how would you do this in code? Ask, what is available in Blitz to let me do this. Do I need to do the math myself, or is there a built in 'distance' function...?

Take the thought above and refine it a bit closer to real code:
If functionCheckDistanceBetween2Points ( (point1 = Player\LocationXYZ), (point2 = Weapon\LocationXYZ) ) returns a value that is less than theWeapon\maxPickupDistance then
do something!

Continuing to work through the problem:
-Check to see if they are looking at the weapon. (Do you want it in view, in the center, in an area? all these need to be answered)
-If the weapon is close, and the player has it targeted, then check to see if they have pressed the correct key or mouse button. (useKey, pickupWeapon, fire, you will have to create and map keys to your game commands/inputs.)
-Finally, remove the weapon from the scene, or hide it, and then add a new view weapon of the same kind to the player's inventory.

My 2c,


Jeremy Alessi(Posted 2004) [#16]
I have news ... if you don't have weapon pick ups or weapons switches ... you have not created MOST of any FPS. Maybe you got a camera to walk around and look up and down ... that does not a FPS make.

Hope my code above helped you out because I took a decent amount of time to do that for you.


wizzlefish(Posted 2004) [#17]
Well, I've got the guns made, all the models made, I've got the guns set in the game, and I can switch between them now, all I'm doing is trying to pick them up. And I've got the enemy-shooting part done.....basically just this and the AI, and the AI for this is extremely simple, since the enemies are robots.

And I'll look at your code.


Ross C(Posted 2004) [#18]
Sorry Jeremy, didn't really read much of what was posted :o) Out of sheer lazyness!


wizzlefish(Posted 2004) [#19]
I think I've got it under control....sort of :(


jfk EO-11110(Posted 2004) [#20]
Cool! You still can optimize it.