/*
 * Copyright 2015 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package org.drools.games.pong

import org.drools.games.pong.Ball
import org.drools.games.pong.Bat
import org.drools.games.pong.Player
import org.drools.games.pong.PlayerId
import org.drools.games.pong.PongGame
import org.drools.games.pong.PointWin
import org.drools.games.KeyPressed
import org.drools.games.pong.Collision
import org.drools.games.pong.CollisionType

import org.drools.games.Run;

import java.lang.Math

import java.awt.Color
import java.awt.Graphics
import java.awt.event.KeyEvent

global PongConfiguration pconf

dialect "mvel"


rule MoveBall agenda-group "Move" when
    ui : PongUI( )
    ball : Ball()
    $r : Run()
then
    modify( ball ) { x =  ball.x + ( ball.dx * ball.speed), y =  ball.y + ( ball.dy * ball.speed )};
end

rule "Detect Move Bat Up"  agenda-group "Move" when
    ui : PongUI( )
    ( ( KeyPressed( keyText == "A" ) and  bat : Bat( playerId == PlayerId.PlayerOne ) )
       or
      (KeyPressed( keyText == "K" ) and  bat : Bat( playerId == PlayerId.PlayerTwo ) )
    )
then
    modify( bat ) { dy = 0 - bat.speed };
end

rule "Undetect Move Bat Up"  agenda-group "Move" when
    ui : PongUI( )
    ( ( not KeyPressed( keyText == "A" ) and  bat : Bat( playerId == PlayerId.PlayerOne ) )
       or
      ( not KeyPressed( keyText == "K" ) and  bat : Bat( playerId == PlayerId.PlayerTwo ) )
    )
then
    modify( bat ) { dy = 0 };
end

rule "Detect Move Bat Down"  agenda-group "Move" when
    ui : PongUI( )
    ( ( KeyPressed( keyText == "Z" ) and  bat : Bat( playerId == PlayerId.PlayerOne ) )
       or
      (KeyPressed( keyText == "M" ) and  bat : Bat( playerId == PlayerId.PlayerTwo ) )
    )
then
    modify( bat ) { dy = bat.speed  };
end


rule "Undetect Move Bat Down"  agenda-group "Move" when
    ui : PongUI( )
    ( ( not KeyPressed( keyText == "Z" ) and  bat : Bat( playerId == PlayerId.PlayerOne ) )
       or
      ( not KeyPressed( keyText == "M" ) and  bat : Bat( playerId == PlayerId.PlayerTwo ) )
    )
then
    modify( bat ) { dy = 0 };
end


rule "Move Bat" agenda-group "Move" when
    ui : PongUI( )
    bat : Bat()
    $r : Run()
    if ( bat.y + bat.dy <= pconf.boundedTop ) break[boundedTop]
    if ( bat.y + bat.dy + bat.height >= pconf.boundedBottom) break[boundedBottom]
then
    modify( bat ) { y =  bat.y + bat.dy };
then[boundedTop]
    modify( bat ) { y =  pconf.boundedTop };
then[boundedBottom]
    modify( bat ) { y =  pconf.boundedBottom - pconf.batHeight   };
end
