Ce qui pleurent que PB à de la 2D Has-Been ( même si ils ont pas tout à fait tord ) , non plus de raison de l'être.
La technique est simple on affiche des plans 2D tout bêtement.
pour la physique , PB permet de contraindre les objets sur certain axes ( en mouvement linéaire & en rotation )
j'utilise donc pour la physique du petit jeu , des cube de 1 unité de coté que j'ai contraint sur les axes , je vous laisse découvrir le code qui à la base
me servais pour le punch en cour , flèches & espace pour jouer :
sheet.png
Code : Tout sélectionner
IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"
Global Quit.b = #False
Structure Tile
CollideEntity.l
TileEntity.l
life.l
autotile.b
climb.b
EndStructure
Structure Destructible
ID.l
x.l:y.l
explode.b
explodeForce.f
scrapType.l
EndStructure
NewList Destructible.Destructible()
Structure pt
ent.l
mat.l
px.d
py.d
ox.d
oy.d
v.d
a.d
t.d
ttl.l
EndStructure
Global NewList Particle.pt()
Global Dim Pixels.i(1920,1080)
Global Dim ground.Tile(512,128)
CamX.d:CamY.d = 128:CamZ.d = 100
If InitEngine3D():InitSprite():InitKeyboard():InitJoystick()
If Screen3DRequester()
Declare.i CreateTile(Size.d)
Declare.d LinearInterpolate(y1.d,y2.d,mu.d)
Declare CreateParticle(x.f,y.f,angle.f,mat.i,vx.d,vy.d)
; Create tile
;
Add3DArchive("./",#PB_3DArchive_FileSystem)
SHEET = LoadTexture(#PB_Any,"sheet.png")
StartDrawing(TextureOutput(SHEET))
For y = 0 To 1080-1
For x = 0 To 1920-1
DrawingMode(#PB_2DDrawing_AlphaChannel)
Pixels(x,y) = Point(x,y)
Next
Next
StopDrawing()
index = 0
For ty = 0 To 8
For tx = 0 To 15
CreateTexture(index,32,32)
StartDrawing(TextureOutput(index))
DrawingMode(#PB_2DDrawing_AlphaChannel):Box(0,0,32,32,RGBA(0,0,0,0))
DrawingMode(#PB_2DDrawing_AlphaBlend)
For y = 0 To 31
For x = 0 To 31
Plot(x,31-y,Pixels(x+(tx*32),y+(ty*32)))
Next
Next
StopDrawing()
CreateMaterial(index,TextureID(index))
MaterialBlendingMode(index,#PB_Material_AlphaBlend)
MaterialFilteringMode(index, #PB_Material_None)
index+1
Next
Next
BG_TEX = CreateTexture(#PB_Any,240,270)
StartDrawing(TextureOutput(BG_TEX))
DrawingMode(#PB_2DDrawing_AlphaChannel)
Box(0,0,240,270,RGBA(0,0,0,0))
DrawingMode(#PB_2DDrawing_AlphaBlend)
For y = 0 To 270-1
For x = 0 To 240-1
Plot(x,269-y,Pixels(x+960,y))
Next
Next
StopDrawing()
m = CreateMaterial(#PB_Any,TextureID(BG_TEX))
MaterialBlendingMode(m,#PB_Material_AlphaBlend)
MaterialFilteringMode(m, #PB_Material_None)
BG_TEX = CreateTexture(#PB_Any,240,270)
StartDrawing(TextureOutput(BG_TEX))
DrawingMode(#PB_2DDrawing_AlphaChannel)
Box(0,0,240,270,RGBA(0,0,0,0))
DrawingMode(#PB_2DDrawing_AlphaBlend)
For y = 0 To 270-1
For x = 0 To 240-1
Plot(x,269-y,Pixels(x+1200,y))
Next
Next
StopDrawing()
mb = CreateMaterial(#PB_Any,TextureID(BG_TEX))
MaterialBlendingMode(mb,#PB_Material_AlphaBlend)
MaterialFilteringMode(mb, #PB_Material_None)
For i = 0 To 20
PlaneMesh = CreatePlane(#PB_Any,240,270,1,1,1,1)
e = CreateEntity(#PB_Any,MeshID(PlaneMesh),MaterialID(mb),(-i*240)+Random(120),135-70+Random(140),-250)
RotateEntity(e,90,0,0)
e = CreateEntity(#PB_Any,MeshID(PlaneMesh),MaterialID(m),(-i*340)+Random(220),135-70+Random(140),-450)
RotateEntity(e,90,0,0)
Next
INVISIBLE_TEXTURE = CreateTexture(#PB_Any,64,64)
StartDrawing(TextureOutput(INVISIBLE_TEXTURE))
DrawingMode(#PB_2DDrawing_AlphaChannel)
Box(0,0,64,64,RGBA(0,0,0,0))
; DrawingMode(#PB_2DDrawing_AlphaBlend)
; Box(0,0,64,64,RGBA(255,0,0,64))
StopDrawing()
INVISIBLE_MATERIAL = CreateMaterial(#PB_Any,TextureID(INVISIBLE_TEXTURE))
MaterialBlendingMode(INVISIBLE_MATERIAL,#PB_Material_AlphaBlend)
; create tile mesh
;
TILE_MESH = CreateTile(1)
CUBE_MESH = CreateCube(#PB_Any,1)
MCUBE_MESH = CreateCube(#PB_Any,0.5)
For y = 0 To 127
For x = 0 To 511
value.i = Pixels(x,(1080-127)+y)
If value = $FFFFFFFF
ground(x,y)\autotile = #True
ground(x,y)\life = -1
ground(x,y)\TileEntity = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(0),x,128-y,0)
ground(x,y)\CollideEntity = CreateEntity(#PB_Any,MeshID(CUBE_MESH),MaterialID(INVISIBLE_MATERIAL),x,128-y,0)
EntityPhysicBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0,0.1,0.5)
; CreateEntityBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0,0.1,0.5)
EndIf
If value = $FF002080
ground(x,y)\life = 8
ground(x,y)\TileEntity = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(48))
ground(x,y)\CollideEntity = CreateEntity(#PB_Any,MeshID(CUBE_MESH),MaterialID(INVISIBLE_MATERIAL),x,128-y,0)
AttachEntityObject(ground(x,y)\CollideEntity,"",EntityID(ground(x,y)\TileEntity))
EntityPhysicBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0.8,0,0.5)
; CreateEntityBody(ground(x,y)\CollideEntity,#PB_Entity_BoxBody,0.8,0,0.5)
EntityAngularFactor(ground(x,y)\CollideEntity,0,0,1)
EntityLinearFactor(ground(x,y)\CollideEntity,1,1,0)
AddElement(Destructible())
Destructible()\ID = ground(x,y)\CollideEntity
Destructible()\x = x:Destructible()\y = y
Destructible()\scrapType = 1 ; wood
EndIf
If value = $FF808080
ground(x,y)\climb = #True
ground(x,y)\TileEntity = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(41+Random(6,0)),x,128-y,-0.1)
EndIf
Next
Next
;autotile
For y = 0 To 127
For x = 0 To 511
;level
If ground(x,y)\autotile
tile_value.l = 0
If y-1 =>0 : If ground(x,y-1)\autotile = 1 : tile_value + 1 : EndIf : EndIf
If y+1 <128 : If ground(x,y+1)\autotile = 1 : tile_value + 4 : EndIf : EndIf
If x-1 =>0 : If ground(x-1,y)\autotile = 1 : tile_value + 8 : EndIf : EndIf
If x+1 <512 : If ground(x+1,y)\autotile = 1 : tile_value + 2 : EndIf : EndIf
SetEntityMaterial(ground(x,y)\TileEntity,MaterialID(tile_value))
EndIf
Next
Next
TILE_MESH = CreateSphere(#PB_Any,0.49,32,32);CreateTile(0.8)
D = CreateEntity(#PB_Any,MeshID(TILE_MESH),MaterialID(INVISIBLE_MATERIAL),0,128,0)
EntityPhysicBody(D,#PB_Entity_SphereBody,1,0.1,0.5)
; CreateEntityBody(D,#PB_Entity_SphereBody,1,0.1,0.5)
EntityAngularFactor(D,0,0,0)
EntityLinearFactor(D,1,1,0)
T = CreateTile(1)
TM = CreateEntity(#PB_Any,MeshID(T),MaterialID(32))
AttachEntityObject(D,"",EntityID(TM))
WorldGravity(-9.81)
Camera2D = CreateCamera(#PB_Any,0,0,100,100)
CameraBackColor(Camera2D,RGB(255,128,64))
CameraRange(Camera2D,0.01,1000)
CameraFOV(Camera2D,40)
AmbientColor(RGB(255,128,64))
; CreateLight(0,$CACACA,100,100,100)
deltaTime.f = 0
While #True
GameClock = ElapsedMilliseconds()
Screen3DEvents()
If Quit Or KeyboardPushed(#PB_Key_Escape)
Break
EndIf
If KeyboardPushed(#PB_Key_Right) And GetEntityAttribute(D,#PB_Entity_LinearVelocityX) < 4
ApplyEntityForce(D,10,0,0)
EndIf
If KeyboardPushed(#PB_Key_Left) And GetEntityAttribute(D,#PB_Entity_LinearVelocityX) > -4
ApplyEntityForce(D,-10,0,0)
EndIf
Tx = Int(EntityX(D)+0.5)
Ty = (128)-Int(EntityY(D))
If Tx<0 : Tx=0 : EndIf
If Ty<0 : Ty=0 : EndIf
If Tx>511 : Tx=511 : EndIf
If Ty>127 : Ty=127 : EndIf
If ground(Tx,Ty)\climb = 1 And kf = #False
If GetEntityAttribute(D,#PB_Entity_LinearVelocityY)<=1.8
EntityVelocity(D, GetEntityAttribute(D,#PB_Entity_LinearVelocityX)/1.1 , 0, 0)
EntityLinearFactor(D,1,0,0)
EndIf
ElseIf ground(Tx,Ty)\climb = 0
EntityLinearFactor(D,1,1,0)
ElseIf ground(Tx,Ty)\climb = 1 And kf = #True
If GetEntityAttribute(D,#PB_Entity_LinearVelocityY)<=1.8
EntityLinearFactor(D,1,1,0)
ApplyEntityImpulse(D,0,7.5,0)
kf = #False
EndIf
EndIf
If KeyboardPushed(#PB_Key_Up) And ground(Tx,Ty)\climb
MoveEntity(D,0,0.1*deltaTime,0)
EndIf
If KeyboardPushed(#PB_Key_Down) And ground(Tx,Ty)\climb
MoveEntity(D,0,-0.1*deltaTime,0)
EndIf
If KeyboardPushed(#PB_Key_Space) And kf = #False And Abs(GetEntityAttribute(D,#PB_Entity_LinearVelocityY)) < 0.005
kf = #True
ApplyEntityImpulse(D,0,7.5,0)
EndIf
If Not KeyboardPushed(#PB_Key_Space) And kf=#True
kf=#False
EndIf
SetEntityAttribute(D,#PB_Entity_LinearSleeping,0)
SetEntityAttribute(D,#PB_Entity_AngularSleeping,0)
ExamineWorldCollisions(1)
While NextWorldCollision()
ForEach Destructible()
If Destructible()\ID = FirstWorldCollisionEntity()
WorldCollisionAppliedImpulse()
ground( Destructible()\x,Destructible()\y)\life - GetY()
If ground( Destructible()\x,Destructible()\y)\life < 0
For i = 0 To 9
vx.d=GetEntityAttribute(Destructible()\ID,#PB_Entity_LinearVelocityX)
vy.d=GetEntityAttribute(Destructible()\ID,#PB_Entity_LinearVelocityY)
CreateParticle(EntityX(Destructible()\ID),EntityY(Destructible()\ID),90,49+Random(2),vx,vy)
Next
FreeEntity( ground( Destructible()\x,Destructible()\y)\CollideEntity)
FreeEntity( ground( Destructible()\x,Destructible()\y)\TileEntity)
DeleteElement(Destructible())
Break 2
EndIf
EndIf
Next
Wend
ForEach Particle()
If(Particle()\ttl<ElapsedMilliseconds())
FreeEntity(Particle()\ent)
DeleteElement(Particle(),1)
EndIf
Next
CamX = LinearInterpolate(CamX,EntityX(D),deltaTime/600)
CamY = LinearInterpolate(CamY,EntityY(D),deltaTime/200)
MoveCamera(Camera2D,CamX,CamY,10,#PB_Absolute)
CameraLookAt(Camera2D,EntityX(D),EntityY(D),0)
RenderWorld(deltaTime*2) ; Draw 3d stuff
; Draw hud
FlipBuffers()
deltaTime = (ElapsedMilliseconds() - GameClock)
Wend
EndIf
Else
MessageRequester("Error","Engine3D.dll error when loading.")
End
EndIf
Procedure.i CreateTile(Size.d)
TILE_MESH = CreateMesh(#PB_Any)
MeshVertexPosition(-(Size/2), -(Size/2), 0)
MeshVertexTextureCoordinate(0, 0)
MeshVertexPosition((Size/2), -(Size/2), 0)
MeshVertexTextureCoordinate(1, 0)
MeshVertexPosition((Size/2), (Size/2), 0)
MeshVertexTextureCoordinate(1, 1)
MeshVertexPosition(-(Size/2), (Size/2), 0)
MeshVertexTextureCoordinate(0, 1)
MeshFace(0,1,2)
MeshFace(3,0,2)
FinishMesh(#True)
NormalizeMesh(TILE_MESH)
UpdateMeshBoundingBox(TILE_MESH)
ProcedureReturn TILE_MESH
EndProcedure
Procedure.d LinearInterpolate(y1.d,y2.d,mu.d)
ProcedureReturn(y1*(1-mu)+y2*mu)
EndProcedure
Procedure CreateParticle(x.f,y.f,angle.f,mat.i,vx.d,vy.d)
AddElement(Particle())
Particle()\ox = (x-0.5)+((10-Random(10))/10)
Particle()\oy = (y-0.5)+((10-Random(10))/10)
Particle()\ttl = ElapsedMilliseconds()+3000
Particle()\ent = CreateEntity(#PB_Any,MeshID(CreateTile(1)),MaterialID(mat),Particle()\ox,Particle()\oy,0.5)
EntityAngularFactor(Particle()\ent,0,0,1)
EntityLinearFactor(Particle()\ent,1,1,0)
EntityPhysicBody(Particle()\ent,#PB_Entity_BoxBody,4,1,0)
ApplyEntityImpulse(Particle()\ent,(((-10+Random(20))/10)*3)+vx,(((-10+Random(20))/10)*10)+vy,0)
SetEntityCollisionFilter(Particle()\ent, 1, $1001)
EndProcedure