/** An object, which moves about it straight lines. The Player must avoid thoughing these (unless the Rock and the Player are the same clan aka same colour). */ class Rock : AbstractTaggedRole { @Attribute val velocity = Vector2(1.0, 1.0) @Attribute var clan = Clan.MO @Attribute var rotation = 0.0 var play : Play var mainView : StageView = null override fun activated() { play = Play.instance actor.color = clan.color.copy() // Tagged with all clans EXCEPT my own, so that Player need only check for collisions // with his clan. tags.addAll( Clan.EENY, Clan.MEENY, Clan.MINY, Clan.MO ) tags.remove( clan ) mainView = Game.instance.scene.findView("main") as StageView } override fun tick() { val margin = 30 // Slightly less thatn the width of the edges actor.direction.degrees += rotation / 60 actor.position += velocity val gameInfo = Game.instance.resources.gameInfo // Normalise the position to 0..width and 0..height while (actor.x < 0) actor.x += gameInfo.width while (actor.y < 0) actor.y += gameInfo.height while (actor.x > gameInfo.width) actor.x -= gameInfo.width while (actor.y > gameInfo.height) actor.y -= gameInfo.height val worldRect = actor.appearance.worldRect() if (play.horizontalClan != clan) { // Bounce off the edge of the screen? if (worldRect.left < margin && velocity.x < 0) { velocity.x = - velocity.x actor.event("wall") } else if (worldRect.right > gameInfo.width - margin && velocity.x > 0) { velocity.x = - velocity.x actor.event("wall") } } if (play.verticalClan != clan) { // Bounce off the edge of the screen? if (worldRect.bottom < margin && velocity.y < 0) { velocity.y = - velocity.y actor.event("wall") } else if (worldRect.top > gameInfo.height - margin && velocity.y > 0) { velocity.y = - velocity.y actor.event("wall") } } } }