mnit_ballz: game_logic module
authorBlackMinou <romain.chanoir@viacesi.fr>
Sun, 21 Jun 2015 23:59:24 +0000 (01:59 +0200)
committerBlackMinou <romain.chanoir@viacesi.fr>
Wed, 9 Sep 2015 08:34:48 +0000 (10:34 +0200)
Signed-off-by: BlackMinou <romain.chanoir@viacesi.fr>

examples/mnit_ballz/src/game_logic.nit

index 7ece167..38d7682 100644 (file)
@@ -1,7 +1,5 @@
 #this file is part of NIT ( http://www.nitlanguage.org ).
 #
-# Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
-#
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+# All the logic of the game
 module game_logic
 
-import mnit::android
-import android::sensors
+import assets
+import objects
+import geometry::quadtree
+import collision
 
-class Ball
-       var x: Float
-       var y: Float
-       var dim: Int
-       var walls_activated: Bool
-       var offset_x = 0.0
-       var offset_y = 0.0
-       var going_left: Bool
-       var going_down: Bool
+redef class Ball
 
-       var game: Game
+       # Scale for drawing the image of `self`
+       var scale = 1.0
 
-       init(game: Game, x,y: Float, walls: Bool)
-       do
-               self.x = x
-               self.y = y
-               self.dim = 20
-               self.game = game
-               self.walls_activated = walls
-       end
+       # ASensorProximity value for modifying `scale`
+       # Not used yet
+       var scale_proximity_modifier = 6.0
+
+       # Radius of `self`
+       var radius = 32.0
+
+       # Movement vector of `self`
+       var offset = new Point[Float](0.0, 0.0)
 
-       # not very useful at this time
-       fun do_turn
+       # Calculates the acceleration of `self`
+       fun acceleration(x,y: Float)
        do
+               var max_offset = 10.0
+               var max_value = 9.80
+               var offset_x = offset.x - x/max_value
+               var offset_y = offset.y + y/max_value
+               if offset_x > max_offset then offset_x = max_offset
+               if offset_x < -max_offset then offset_x = -max_offset
+               if offset_y > max_offset then offset_y = max_offset
+               if offset_y < -max_offset then offset_y = -max_offset
+               offset = new Point[Float](offset_x, offset_y)
        end
 
-       fun intercepts(event: InputEvent): Bool
+       # Do the collision detection, then move `self`consequently
+       fun do_turn(game: Game)
        do
-               if event isa ASensorAccelerometer then
-                       do_move(event)
-               else if event isa ASensorMagneticField then
-                       #deal with Magnetic field sensor
-                       print "ASensorMagneticField : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
-               else if event isa ASensorGyroscope then
-                       #deal with Gyroscope sensor
-                       print "ASensorGyroscope : x = " + event.x.to_s + " y = " + event.y.to_s + " z = " + event.z.to_s
-               else if event isa ASensorLight then
-                       #deal with light sensor
-                       print "ASensorLight : light = " + event.light.to_s
-               else if event isa ASensorProximity then
-                       #deal with proximity sensor
-                       print "ASensorProximity : distance = " + event.distance.to_s
-               else if event isa MotionEvent then
-                       activate_walls(event)
+               offset = new Point[Float](offset.x * 0.98, offset.y * 0.98)
+               var np = collision(game.quadtree)
+               if np != null then
+                       offset = np
+                       center = new Point[Float](center.x + offset.x, center.y + offset.y)
+               else
+                       center = new Point[Float](center.x + offset.x, center.y + offset.y)
                end
-               return true
        end
 
-       fun do_move (event: ASensorAccelerometer)
+       # Collision detection
+       fun collision(quadtree: SQuadTree[OrientedLine]): nullable Point[Float]
        do
-               # acceleration value
-               var vx = event.x
-               var vy = event.y
 
-               var gw = game.width
-               var gh = game.height
-
-               # acceleration
-               var max_value = 9.80
-               var acceleration_x = vx/max_value
-               var acceleration_y = vy/max_value
-               offset_x -= (acceleration_x/10.0)*(vx.abs) + offset_x/125.0
-               offset_y += (acceleration_y/10.0)*(vy.abs) - offset_y/125.0
-               var nx = self.x + offset_x
-               var ny = self.y + offset_y
-               going_left = offset_x > 0.0
-               going_down = offset_y > 0.0
-
-               # x value
-               if nx >= 0.0 and nx <= gw then
-                       self.x = nx
-               else if nx < 0.0 then
-                       if not walls_activated then self.x = gw else do_bounce(1)
-               else if nx > gw then
-                       if not walls_activated then self.x = 0.0 else do_bounce(1)
+               var nx = self.center.x + offset.x
+               var ny = self.center.y + offset.y
+               var new_center = new Point[Float](nx, ny)
+               var effective_radius = radius*scale
+               # Lines intersecting with the ball
+               var intersecting_lines = new Array[OrientedLine]
+
+               # Line intersecting closest to the ball
+               var intersecting_line: nullable OrientedLine = null
+
+               # closest point of the intersecting line
+               var closest_point: nullable Point[Float] = null
+
+               # get the intersecting lines with help of the quadtree
+               var lines = quadtree.items_overlapping(new_center.padded(effective_radius))
+               for l in lines do
+                       if is_intersecting(new_center, l.point_left, l.point_right, effective_radius) then
+                               intersecting_lines.add(l)
+                       end
                end
 
-               # y value
-               if ny >= 0.0 and ny <= gh then
-                       self.y = ny
-               else if ny < 0.0 then
-                       if not walls_activated then self.y = gh else do_bounce(2)
-               else if ny > gh then
-                       if not walls_activated then self.y = 0.0 else do_bounce(2)
+               # get the line closest to the ball from the intersecting lines, setting the closest point
+               var min_dist = 100.0
+               if intersecting_lines.length >= 2 then
+                       for l in intersecting_lines do
+                               var closest = point_closest_to_line(new_center, l.point_left, l.point_right)
+                               var distance = distance(closest, new_center)
+                               if distance < min_dist then
+                                       min_dist = distance
+                                       intersecting_line = l
+                                       closest_point = closest
+                               end
+                       end
+               else if intersecting_lines.length == 1 then
+                       intersecting_line = intersecting_lines[0]
+                       closest_point = point_closest_to_line(new_center, intersecting_line.point_left, intersecting_line.point_right)
                end
-       end
 
-       # bounce in function of the position of the wall relative to the ball: 1=left or right, 2=top or down
-       fun do_bounce(wall_position: Int)
-       do
-               if wall_position == 1 then
-                       offset_x = -offset_x*0.85
-               else if wall_position == 2 then
-                       offset_y = -offset_y*0.85
-               end
-               if offset_x.abs > 1.0 and offset_y.abs > 1.0 then
-               self.x += offset_x
-               self.y += offset_y
+               if intersecting_line != null and closest_point != null then
+                       return bounce(center, intersecting_line.point_left, intersecting_line.point_right, offset)
                end
+               return null
        end
 
-       fun activate_walls(event: MotionEvent)
-       do
-               if event.just_went_down then
-                       walls_activated = not walls_activated
-               end
-       end
+       # Event interception
+       fun intercepts(event: InputEvent): Bool is abstract
 end
 
-class Screen
-       var ball_img: Image
-       var game: Game
-
-       init(app: App, display: Display)
-       do
-               game = new Game(display)
-               ball_img = app.load_asset("images/ball.png").as(Image)
-               var scale = game.img_dim.to_f / game.img_ori_dim.to_f
-               ball_img.scale = scale
-               ball_img.scale = 3.0
-       end
+# The core of the game
+class Game
 
-       fun do_frame(display: Display)
-       do
-               display.clear(0.0, 0.0, 0.0)
-               display.blit_rotated(ball_img, game.ball.x, game.ball.y, 0.0)
-       end
+       # The Ball!
+       var ball: Ball is noinit
 
-       fun input(ie: InputEvent): Bool
-       do
-               if ie isa ASensorProximity then
-                       if ie.distance == 0.0 then ball_img.scale = 6.0 else ball_img.scale = 3.0
-               else
-                       game.ball.intercepts(ie)
-               end
-               return true
-       end
-end
+       # List of walls in the level
+       var walls: Array[Wall] is noinit
 
-class Game
-       var ball: Ball
+       # Width of the display
        var width: Float
+
+       # Heightof the display
        var height: Float
 
-       var img_ori_dim: Int = 256
-       fun img_dim: Int do return 210
+       # Quadtree used for collision detection
+       var quadtree: SQuadTree[OrientedLine] is noinit
 
-       init(display: Display)
+       init
        do
-               width = display.width.to_f
-               height = display.height.to_f
-               ball = new Ball(self, width/2.0, height/2.0, false)
+               ball = new Ball(new Point[Float](width/2.0, height/2.0))
+               # Walls initialisation
+               var walla = new Wall(new Point[Float](width/4.0, height/4.0), pi/3.0, 1.0)
+               var wallb = new Wall(new Point[Float](width*0.75, height/4.0), 0.0, 1.0)
+               var wallc = new Wall(new Point[Float](width/4.0, height*0.75), 0.0, 1.0)
+               var walld = new Wall(new Point[Float](width*0.75, height*0.75), pi/3.0, 1.0)
+               walls = new Array[Wall].with_items(walla, wallb, wallc, walld)
+
+               # adding screen bordures
+               var i = new Point[Float](0.0,0.0)
+               var a = new Point[Float](0.0, height/2.0)
+               var b = new Point[Float](width/2.0, 0.0)
+               var c = new Point[Float](width, height/2.0)
+               var d = new Point[Float](width/2.0, height)
+
+               var l1 = new OrientedLine(i, i, pi/2.0, height, a)
+               var l2 = new OrientedLine(i, i, 0.0, width, b)
+               var l3 = new OrientedLine(i, i, pi/2.0, height, c)
+               var l4 = new OrientedLine(i, i, 0.0, width, d)
+
+               quadtree = new SQuadTree[OrientedLine](5, width, height)
+               for w in walls do for l in w.lines do
+                       quadtree.add(l)
+               end
+               quadtree.add(l1)
+               quadtree.add(l2)
+               quadtree.add(l3)
+               quadtree.add(l4)
        end
 
-       fun do_turn
-       do
-       ball.do_turn
+       # Only calls `do_turn` of the ball for the moment
+       fun do_turn do ball.do_turn(self)
+
+       # Input gestion
+       fun input(ie: InputEvent): Bool do return false
+end
+
+redef class App
+
+       # Assets used in all the app
+       var assets = new Assets
+
+       redef fun on_create do
+               super
+               assets.load
        end
 end