Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package my.game.pkg.ship
- import my.game.pkg.utils.Implicits._
- import com.badlogic.gdx.Gdx
- import com.badlogic.gdx.graphics.Texture
- import com.badlogic.gdx.graphics.g2d.Batch
- import com.badlogic.gdx.graphics.g2d.SpriteBatch
- import com.badlogic.gdx.scenes.scene2d.Actor
- import com.badlogic.gdx.scenes.scene2d.Stage
- import com.badlogic.gdx.scenes.scene2d.actions.Actions
- import com.badlogic.gdx.scenes.scene2d.Action
- import com.badlogic.gdx.scenes.scene2d.InputEvent
- import com.badlogic.gdx.scenes.scene2d.InputListener
- import com.badlogic.gdx.scenes.scene2d.actions.RotateByAction
- import com.badlogic.gdx.math.Vector2
- import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction
- import scala.language.implicitConversions._
- import scala.collection.JavaConversions._
- import scala.math
- class ShipMovingAction(val acc:Float) extends Action {
- override def act(delta: Float):Boolean = {
- def proj(angle:Float, value:Float): (Float, Float) = {
- (value * math.cos(angle), value * math.sin(angle))
- }
- val ship = actor match {
- case ship:Ship => ship
- case _ => throw new ClassCastException
- }
- val old_velocity = ship.velocity
- println(old_velocity)
- println(acc)
- if(acc < 0 && math.abs(old_velocity.x) < 0.01 && math.abs(old_velocity.y) < 0.01) {
- println("HERE")
- return true
- }
- val proj_a = proj(ship.getRotation * math.Pi / 180, acc)
- val new_velocity = new Vector2(old_velocity.x + proj_a._1 * delta,
- old_velocity.y + proj_a._2 * delta)
- val s_x = old_velocity.x * delta + (proj_a._1 * delta * delta) / 2
- val s_y = old_velocity.y * delta + (proj_a._2 * delta * delta) / 2
- // println(proj_a)
- // println(old_velocity)
- // println(new_velocity)
- // println(s_x)
- // println(s_y)
- // println(ship.getRotation)
- ship.velocity = new_velocity
- ship.addAction(Actions.moveBy(-s_x, -s_y))
- true
- }
- }
- class Ship extends Actor {
- val texture = new Texture(Gdx.files.internal("ship.png"))
- setBounds(0,0,texture.getWidth(), texture.getHeight())
- setOrigin(getWidth()/2, getHeight()/2);
- var velocity: Vector2 = new Vector2(0f, 0f)
- var movement_action = new Action {
- override def act(delta:Float) = {
- // val direction = (new Vector2(-1, 0)).rotate(getRotation())
- moveBy(-velocity.x, -velocity.y)
- true
- }
- }
- def resetState() = {
- addAction(movement_action)
- addAction(new ShipMovingAction(-0.5f))
- }
- resetState()
- addListener(new InputListener {
- override def keyDown(event: InputEvent, keycode: Int):Boolean = {
- movement(keycode)
- true
- }
- override def keyUp(event: InputEvent, keycode: Int):Boolean = {
- clearActions()
- resetState()
- true
- }
- })
- override def draw(batch:Batch, alpha:Float) {
- batch.draw(texture,this.getX(),getY(),this.getOriginX(),this.getOriginY(),this.getWidth(),
- this.getHeight(),this.getScaleX(), this.getScaleY(),this.getRotation(),0,0,
- texture.getWidth(),texture.getHeight(),false,false);
- }
- def movement(keycode:Int) = {
- def rotate_amount(amount:Int) = {
- Actions.forever(Actions.rotateBy(amount))
- }
- def forward() = {
- // math.cos()
- // val velScale = 1.0f
- // val rotation = this.getRotation() * math.Pi / 180
- // println(rotation)
- // println(math.cos(rotation))
- // velocity.x = velocity.x + math.cos(rotation) * velScale
- // velocity.y = velocity.y - math.sin(rotation) * velScale
- // val action = Actions.moveBy(1, 0)
- // Actions.moveBy(velocity.x, velocity.y)
- }
- keycode match {
- // left
- case 21 => {
- this.addAction(rotate_amount(10))
- }
- // right
- case 22=> {
- this.addAction(rotate_amount(-10))
- }
- // up
- case 19 => {
- this.addAction(Actions.forever(new ShipMovingAction(2.0f)))
- }
- case _ =>
- }
- }
- override def act(delta: Float) {
- for(i <- getActions()) {
- i.act(delta)
- }
- if(getX() > getStage().getViewport().getViewportWidth()) {
- setX(0)
- }
- if(getX() < 0) {
- setX(getStage().getViewport().getViewportWidth())
- }
- if(getY() < 0) {
- setY(getStage().getViewport().getViewportHeight())
- }
- if(getY() > getStage().getViewport().getViewportHeight()) {
- setY(0)
- }
- setX(getX())
- }
- // implicit def float2Double(v:float):Double = v.toDouble
- implicit def double2Float(v: Double): Float = v.toFloat
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement