Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TDT4240 Tank Wars
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Snorre Skjellestad Kristiansen
TDT4240 Tank Wars
Commits
6a38f776
Commit
6a38f776
authored
2 years ago
by
Magnus Segtnan Skjølberg
Browse files
Options
Downloads
Patches
Plain Diff
(
#18
): add camera for hud and world + scaling of camera
parent
e1ef339e
No related branches found
No related tags found
3 merge requests
!51
Resolve "Adjust cannon angle from touch input"
,
!35
Draft: Resolve "Adjust cannon angle from touch input"
,
!32
Combine hud with gameplay
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/core/src/com/game/tankwars/view/GameScreen.java
+30
-25
30 additions, 25 deletions
frontend/core/src/com/game/tankwars/view/GameScreen.java
with
30 additions
and
25 deletions
frontend/core/src/com/game/tankwars/view/GameScreen.java
+
30
−
25
View file @
6a38f776
...
...
@@ -6,19 +6,13 @@ import com.badlogic.gdx.graphics.GL20;
import
com.badlogic.gdx.graphics.Mesh
;
import
com.badlogic.gdx.graphics.OrthographicCamera
;
import
com.badlogic.gdx.graphics.Texture
;
import
com.badlogic.gdx.graphics.g2d.BitmapFont
;
import
com.badlogic.gdx.graphics.g2d.Sprite
;
import
com.badlogic.gdx.graphics.g2d.BitmapFont
;
import
com.badlogic.gdx.graphics.g2d.SpriteBatch
;
import
com.badlogic.gdx.graphics.glutils.ShapeRenderer
;
import
com.badlogic.gdx.math.Vector2
;
import
com.badlogic.gdx.physics.box2d.Body
;
import
com.badlogic.gdx.physics.box2d.Box2DDebugRenderer
;
import
com.badlogic.gdx.physics.box2d.World
;
import
com.badlogic.gdx.utils.Array
;
import
com.badlogic.gdx.utils.ScreenUtils
;
import
com.badlogic.gdx.utils.viewport.FitViewport
;
import
com.badlogic.gdx.utils.viewport.Viewport
;
import
com.badlogic.gdx.utils.viewport.FitViewport
;
import
com.game.tankwars.TankWarsGame
;
import
com.game.tankwars.controller.GameController
;
...
...
@@ -29,8 +23,7 @@ import com.game.tankwars.model.Terrain;
public
class
GameScreen
implements
Screen
{
final
TankWarsGame
tankWarsGame
;
int
VIEWPORT_WIDTH
;
int
VIEWPORT_HEIGHT
;
int
horizontalScaling
;
int
verticalScaling
;
SpriteBatch
batch
;
...
...
@@ -40,7 +33,8 @@ public class GameScreen implements Screen {
Box2dWorld
model
;
World
world
;
Terrain
terrain
;
OrthographicCamera
cam
;
OrthographicCamera
worldCam
;
OrthographicCamera
hudCam
;
Box2DDebugRenderer
debugRenderer
;
Bullet
bullet
;
GameController
controller
;
...
...
@@ -49,30 +43,34 @@ public class GameScreen implements Screen {
public
GameScreen
(
final
TankWarsGame
tankWarsGame
){
this
.
tankWarsGame
=
tankWarsGame
;
VIEWPORT_HEIGHT
=
tankWarsGame
.
getViewportHeight
();
VIEWPORT_WIDTH
=
tankWarsGame
.
getViewportWidth
();
batch
=
new
SpriteBatch
();
shapeRender
=
new
ShapeRenderer
();
model
=
new
Box2dWorld
();
world
=
Box2dWorld
.
getWorld
();
cam
=
new
OrthographicCamera
(
VIEWPORT_WIDTH
,
VIEWPORT_HEIGHT
);
cam
.
position
.
set
(
VIEWPORT_WIDTH
/
2
,
VIEWPORT_HEIGHT
/
2
,
0
);
cam
.
update
();
worldCam
=
new
OrthographicCamera
(
scale
(
TankWarsGame
.
GAMEPORT_WIDTH
),
scale
(
TankWarsGame
.
GAMEPORT_HEIGHT
));
worldCam
.
position
.
set
(
scale
(
TankWarsGame
.
GAMEPORT_WIDTH
)/
2
,
scale
(
TankWarsGame
.
GAMEPORT_HEIGHT
)/
2
,
0
);
hudCam
=
new
OrthographicCamera
(
TankWarsGame
.
GAMEPORT_WIDTH
,
TankWarsGame
.
GAMEPORT_HEIGHT
);
hudCam
.
position
.
set
(
TankWarsGame
.
GAMEPORT_WIDTH
/
2
,
TankWarsGame
.
GAMEPORT_HEIGHT
/
2
,
0
);
worldCam
.
update
();
hudCam
.
update
();
debugRenderer
=
new
Box2DDebugRenderer
(
true
,
true
,
true
,
true
,
true
,
true
);
terrain
=
new
Terrain
();
int
initPos
=
50
;
tank
=
new
Tank
(
initPos
,
new
Texture
(
"tank-khaki.png"
),
new
Texture
(
"cannon.png"
),
terrain
,
tankWarsGame
);
horizontalScaling
=
Gdx
.
graphics
.
getWidth
()
/
VIEWPORT_WIDTH
;
verticalScaling
=
Gdx
.
graphics
.
getHeight
()
/
VIEWPORT_HEIGHT
;
/*
horizontalScaling = Gdx.graphics.getWidth() / VIEWPORT_WIDTH;
verticalScaling = Gdx.graphics.getHeight() / VIEWPORT_HEIGHT;
*/
hud
=
new
GameHud
(
new
FitViewport
(
VIEWPORT_WIDTH
,
VIEWPORT_HEIGHT
,
cam
),
batch
);
hud
=
new
GameHud
(
new
FitViewport
(
TankWarsGame
.
GAMEPORT_WIDTH
,
TankWarsGame
.
GAMEPORT_HEIGHT
,
hudCam
),
batch
);
Gdx
.
input
.
setInputProcessor
(
hud
.
getStage
());
controller
=
new
GameController
(
tankWarsGame
,
hud
,
tank
);
Gdx
.
input
.
setInputProcessor
(
hud
.
getStage
());
controller
.
handleHudEvents
();
}
@Override
...
...
@@ -80,8 +78,8 @@ public class GameScreen implements Screen {
model
.
logicStep
(
Gdx
.
graphics
.
getDeltaTime
());
Gdx
.
gl
.
glClearColor
(
0
,
0
,
100
,
100
);
Gdx
.
gl
.
glClear
(
GL20
.
GL_COLOR_BUFFER_BIT
);
debugRenderer
.
render
(
world
,
c
am
.
combined
);
shapeRender
.
setProjectionMatrix
(
c
am
.
combined
);
debugRenderer
.
render
(
world
,
worldC
am
.
combined
);
shapeRender
.
setProjectionMatrix
(
worldC
am
.
combined
);
controller
.
checkKeyInput
();
...
...
@@ -93,7 +91,7 @@ public class GameScreen implements Screen {
Sprite
s
=
(
Sprite
)
b
.
getUserData
();
if
(
s
!=
null
)
{
s
.
setPosition
(
b
.
getPosition
().
x
*
(
float
)
horizontalScaling
-
s
.
getWidth
()
/
2
,
(
b
.
getPosition
().
y
+
0.25f
)
*
(
float
)
verticalScaling
);
s
.
setPosition
(
b
.
getPosition
().
x
*
(
float
)
TankWarsGame
.
SCALE
-
s
.
getWidth
()
/
2
,
(
b
.
getPosition
().
y
+
0.25f
)
*
(
float
)
TankWarsGame
.
SCALE
);
if
(
s
.
equals
(
tank
.
getChassisSprite
()))
{
s
.
setRotation
(
tank
.
getAngle
());
}
...
...
@@ -102,8 +100,7 @@ public class GameScreen implements Screen {
s
.
setRotation
(
tank
.
getCannonAngle
());
}
}
hud
.
getStage
().
draw
();
}
shapeRender
.
begin
(
ShapeRenderer
.
ShapeType
.
Filled
);
terrain
.
draw
(
shapeRender
);
...
...
@@ -113,12 +110,16 @@ public class GameScreen implements Screen {
tank
.
getChassisSprite
().
draw
(
batch
);
tank
.
getCannonSprite
().
draw
(
batch
);
batch
.
end
();
batch
.
setProjectionMatrix
(
hud
.
getStage
().
getCamera
().
combined
);
hud
.
getStage
().
draw
();
}
@Override
public
void
show
()
{
}
@Override
public
void
resize
(
int
width
,
int
height
)
{
...
...
@@ -147,4 +148,8 @@ public class GameScreen implements Screen {
batch
.
dispose
();
shapeRender
.
dispose
();
}
private
float
scale
(
float
value
)
{
return
value
/
TankWarsGame
.
SCALE
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment