-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.kt
109 lines (94 loc) · 3.44 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.wradchuk.kotlingameengine2d
import android.graphics.Canvas
import android.graphics.Color
import android.view.MotionEvent
import com.wradchuk.game_engine.core.Joystick
import com.wradchuk.game_engine.common.Vector2
import com.wradchuk.game_engine.components.GameLevel
import com.wradchuk.game_engine.components.GameScreen
import com.wradchuk.game_engine.core.GameViewport
import com.wradchuk.game_engine.objects.Camera
import com.wradchuk.game_engine.utils.drawText
import com.wradchuk.game_engine.utils.getBitmap
import com.wradchuk.game_engine.utils.getDisplayHeightInPixels
import com.wradchuk.game_engine.utils.getDisplayWidthInPixels
class MainActivity : GameScreen() {
// Карта тайлов
private val gameLevel by lazy {
GameLevel(
context = this,
bitmap = this.getBitmap(R.drawable.terrain_tiles_v2),
levelData = R.raw.test_level
)
}
// Джойстик для управления
private val joystick by lazy {
Joystick(
outerCircleRadius = 50,
outerCircleCenterPosition = Vector2(80, this.getDisplayHeightInPixels() - 120),
innerCircleRadius = 30,
innerCircleCenterPosition = Vector2(80, this.getDisplayHeightInPixels() - 120),
)
}
// Камера для отображения игрового мира
private val camera by lazy {
Camera(
position = Vector2(1000.0, 1000.0),
context = this,
joystick = joystick,
mapSize = gameLevel.getMapSize(),
)
}
// Отображение игрового мира
private val gameViewport: GameViewport by lazy {
GameViewport(
camera = camera,
widthPixels = this.getDisplayWidthInPixels(),
heightPixels = this.getDisplayHeightInPixels()
)
}
override fun draw(canvas: Canvas) {
gameLevel.drawLevel(canvas, gameViewport)
camera.draw(canvas, gameViewport)
joystick.draw(canvas)
// Рисует текстовые данные о FPS и координатах игровой области
canvas.drawText(
color = Color.MAGENTA,
textSizeF = 30f,
message = "FPS: ${getBaseGame().gameLoop?.averageFPS?.toInt()?.toString()}",
position = Vector2(10f, 30f)
)
canvas.drawText(
color = Color.MAGENTA,
textSizeF = 30f,
message = "x: ${gameViewport.getGameRect().left}, y: ${gameViewport.getGameRect().top}",
position = Vector2(250f, 30f)
)
}
override fun update() {
joystick.update()
camera.update()
gameViewport.update()
}
override fun event(event: MotionEvent): Boolean {
when(event.action) {
MotionEvent.ACTION_DOWN -> {
if(joystick.isPressed(Vector2(event.x.toDouble(), event.y.toDouble()))) {
joystick.setIsPressed(true)
}
return true
}
MotionEvent.ACTION_MOVE -> {
if(joystick.getIsPressed()) {
joystick.setActuator(Vector2(event.x.toDouble(), event.y.toDouble()))
}
return true
}
MotionEvent.ACTION_UP -> {
joystick.setIsPressed(false)
joystick.resetActuator()
}
}
return true
}
}