Advertisement
Guest User

Untitled

a guest
Jun 29th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.00 KB | None | 0 0
  1. package my.game.pkg.ship
  2.  
  3. import my.game.pkg.utils.Implicits._
  4.  
  5. import com.badlogic.gdx.Gdx
  6. import com.badlogic.gdx.graphics.Texture
  7. import com.badlogic.gdx.graphics.g2d.Batch
  8. import com.badlogic.gdx.graphics.g2d.SpriteBatch
  9. import com.badlogic.gdx.scenes.scene2d.Actor
  10. import com.badlogic.gdx.scenes.scene2d.Stage
  11.  
  12. import com.badlogic.gdx.scenes.scene2d.actions.Actions
  13. import com.badlogic.gdx.scenes.scene2d.Action
  14. import com.badlogic.gdx.scenes.scene2d.InputEvent
  15. import com.badlogic.gdx.scenes.scene2d.InputListener
  16. import com.badlogic.gdx.scenes.scene2d.actions.RotateByAction
  17. import com.badlogic.gdx.math.Vector2
  18.  
  19. import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction
  20.  
  21. import scala.language.implicitConversions._
  22.  
  23. import scala.collection.JavaConversions._
  24.  
  25. import scala.math
  26.  
  27. class ShipMovingAction(val acc:Float) extends Action {
  28.     override def act(delta: Float):Boolean = {
  29.         def proj(angle:Float, value:Float): (Float, Float) = {
  30.             (value * math.cos(angle), value * math.sin(angle))
  31.         }
  32.  
  33.         val ship = actor match {
  34.             case ship:Ship => ship
  35.             case _ => throw new ClassCastException
  36.         }
  37.         val old_velocity = ship.velocity
  38.         println(old_velocity)
  39.         println(acc)
  40.         if(acc < 0 && math.abs(old_velocity.x) < 0.01 && math.abs(old_velocity.y) < 0.01) {
  41.             println("HERE")
  42.             return true
  43.         }
  44.         val proj_a = proj(ship.getRotation * math.Pi / 180, acc)
  45.         val new_velocity = new Vector2(old_velocity.x + proj_a._1 * delta,
  46.             old_velocity.y + proj_a._2 * delta)
  47.  
  48.         val s_x = old_velocity.x * delta + (proj_a._1 * delta * delta) / 2
  49.         val s_y = old_velocity.y * delta + (proj_a._2 * delta * delta) / 2
  50.         // println(proj_a)
  51.         // println(old_velocity)
  52.         // println(new_velocity)
  53.         // println(s_x)
  54.         // println(s_y)
  55.         // println(ship.getRotation)
  56.  
  57.         ship.velocity = new_velocity
  58.         ship.addAction(Actions.moveBy(-s_x, -s_y))
  59.         true
  60.     }
  61. }
  62.  
  63. class Ship extends Actor {
  64.  
  65.  
  66.     val texture = new Texture(Gdx.files.internal("ship.png"))
  67.  
  68.     setBounds(0,0,texture.getWidth(), texture.getHeight())
  69.     setOrigin(getWidth()/2, getHeight()/2);
  70.  
  71.     var velocity: Vector2 = new Vector2(0f, 0f)
  72.  
  73.     var movement_action = new Action {
  74.         override def act(delta:Float) = {
  75.             // val direction = (new Vector2(-1, 0)).rotate(getRotation())
  76.             moveBy(-velocity.x, -velocity.y)
  77.             true
  78.         }
  79.     }
  80.     def resetState() = {
  81.         addAction(movement_action)
  82.         addAction(new ShipMovingAction(-0.5f))
  83.     }
  84.  
  85.     resetState()
  86.  
  87.     addListener(new InputListener {
  88.         override def keyDown(event: InputEvent, keycode: Int):Boolean = {
  89.             movement(keycode)
  90.             true
  91.         }
  92.         override def keyUp(event: InputEvent, keycode: Int):Boolean = {
  93.             clearActions()
  94.             resetState()
  95.             true
  96.         }
  97.  
  98.     })
  99.  
  100.     override def draw(batch:Batch, alpha:Float) {
  101.         batch.draw(texture,this.getX(),getY(),this.getOriginX(),this.getOriginY(),this.getWidth(),
  102.             this.getHeight(),this.getScaleX(), this.getScaleY(),this.getRotation(),0,0,
  103.             texture.getWidth(),texture.getHeight(),false,false);
  104.     }
  105.  
  106.     def movement(keycode:Int) = {
  107.         def rotate_amount(amount:Int) = {
  108.             Actions.forever(Actions.rotateBy(amount))
  109.         }
  110.         def forward() = {
  111.             // math.cos()
  112.             // val velScale = 1.0f
  113.             // val rotation = this.getRotation() * math.Pi / 180
  114.             // println(rotation)
  115.             // println(math.cos(rotation))
  116.             // velocity.x = velocity.x + math.cos(rotation) * velScale
  117.             // velocity.y = velocity.y - math.sin(rotation) * velScale
  118.             // val action = Actions.moveBy(1, 0)
  119.             // Actions.moveBy(velocity.x, velocity.y)
  120.         }
  121.         keycode match {
  122.             // left
  123.             case 21 => {
  124.                 this.addAction(rotate_amount(10))
  125.             }
  126.             // right
  127.             case 22=> {
  128.                 this.addAction(rotate_amount(-10))
  129.             }
  130.             // up
  131.             case 19 => {
  132.                 this.addAction(Actions.forever(new ShipMovingAction(2.0f)))
  133.             }
  134.             case _ =>
  135.         }
  136.     }
  137.     override def act(delta: Float) {
  138.         for(i <- getActions()) {
  139.             i.act(delta)
  140.         }
  141.         if(getX() > getStage().getViewport().getViewportWidth()) {
  142.             setX(0)
  143.         }
  144.         if(getX() < 0) {
  145.             setX(getStage().getViewport().getViewportWidth())
  146.         }
  147.         if(getY() < 0) {
  148.             setY(getStage().getViewport().getViewportHeight())
  149.         }
  150.         if(getY() > getStage().getViewport().getViewportHeight()) {
  151.             setY(0)
  152.         }
  153.  
  154.         setX(getX())
  155.     }
  156.     // implicit def float2Double(v:float):Double = v.toDouble
  157.     implicit def double2Float(v: Double): Float = v.toFloat
  158.  
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement