EvgeniyPugach

DZ3

Mar 20th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.00 KB | None | 0 0
  1. import kotlin.math.pow
  2. import kotlin.math.sqrt
  3.  
  4.  
  5. interface Movable {
  6.     fun move()
  7. }
  8.  
  9. class Player(name: String, strength: Int, x: Double, y: Double, width: Int, hight: Int) : GameObject(x, y, width, hight),
  10.         Movable {
  11.     var name: String = ""
  12.         get() = field
  13.         set(value) {
  14.             field = value
  15.         }
  16.  
  17.     override fun move() {
  18.         "Car rides"
  19.     }
  20. }
  21.  
  22. class Enemy(name: String, strength: Int, x: Double, y: Double, width: Int, hight: Int) : GameObject(x, y, width, hight),
  23.         Movable {
  24.     var name: String = ""
  25.         get() = field
  26.         set(value) {
  27.             field = value
  28.         }
  29.  
  30.     override fun move() {
  31.         "Сar rides slow"
  32.     }
  33. }
  34.  
  35. class Block(nature: String, strength: Int, x: Double, y: Double, width: Int, hight: Int) : GameObject(x, y, width, hight) {
  36.     var name: String = ""
  37.         get() = field
  38.         set(value) {
  39.             field = value
  40.         }
  41. }
  42.  
  43. abstract class GameObject(x: Double, y: Double, width: Int, hight: Int) {
  44.  
  45.     private var x: Int = 0
  46.         get() = field
  47.         set(value) {
  48.             field = value
  49.         }
  50.  
  51.     private var y: Int = 0
  52.         get() = field
  53.         set(value) {
  54.             field = value
  55.         }
  56.  
  57.     private var width = 0
  58.         get() = field
  59.         set(value) {
  60.             field = value
  61.         }
  62.  
  63.     private var hight = 0
  64.         get() = field
  65.         set(value) {
  66.             field = value
  67.         }
  68.  
  69.     fun isIntersects(anotherObject: GameObject): Boolean {
  70.         var result : Boolean = false
  71.         if (sqrt((this.x - anotherObject.x).toDouble().pow(2) +
  72.                         (this.y - anotherObject.y).toDouble().pow(2)) == 0.0) result = true
  73.  
  74.         return result
  75.     }
  76. }
  77.  
  78. fun main() {
  79.     var one: Player = Player("BMW", 10, 4.0, 1.0, 25, 50)
  80.     var two: Enemy = Enemy("Honda", 30, 3.0, 5.0, 60, 55)
  81.     var wall: Block = Block("wall",0,4.0,3.0,5,80)
  82.  
  83.     println(one.isIntersects(two))
  84.     println(one.isIntersects(wall))
  85. }
Advertisement
Add Comment
Please, Sign In to add comment