Skip to content

Commit

Permalink
server is no longer a player
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrarush committed Jul 12, 2014
1 parent 7f52637 commit dea3cd1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 57 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#WIP
This project is not finished.
For the time being ships will not be hidden for debugging purposes.

#Description

A modern OpenGL networked battleship game meant to be a working implementation of [userial's app example](https://github.com/nklein/userial#protocol) so that readers may better understand the protocol.
A modern OpenGL networked realtime battleship game meant to be a working implementation of [userial's app example](https://github.com/nklein/userial#protocol) so that readers may better understand the protocol.

The only change to the protocol is that ships will be placed on a field of floats because of OpenGL.

#How to Play

- Left click on your field to place vertical ships
- Right click on your field to place horizontal ships
- Click a ship to remove it
- Left click on the enemy's field to fire missiles
- Right click on the enemy's field to ping
(hold down right click and drag to determine ping radius)
- Right click on the enemy's field to ping (hold down right click and drag to determine ping radius)

#How to Run

Expand Down
123 changes: 70 additions & 53 deletions battleship.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,70 +23,87 @@

(setf *server* server-p)
(if (server-p)
(start-server server-ip port)
(connect-to-server server-ip port))

(sdl2:with-window (win :title (if (server-p) "Battleship Server" "Battleship Client") :w *window-width* :h *window-height* :flags '(:shown :opengl))
(sdl2:with-gl-context (gl-context win)
(sdl2:gl-make-current win gl-context)

(setf *graphics-engine* (make-instance 'graphics-engine))
(graphics-init *graphics-engine*)
(setf *last-time* (sdl2:get-ticks))

(unwind-protect
(sdl2:with-event-loop (:method :poll)
(:keydown
(:keysym keysym)
(let ((scancode (sdl2:scancode-value keysym))
(sym (sdl2:sym-value keysym))
(mod-value (sdl2:mod-value keysym)))))

(:mousebuttondown
(:x x :y y :button button)

;; two vectors needed to trace a ray from mouse click into the 3D world
(multiple-value-bind (v1 v2) (get-3d-ray-under-mouse (ensure-float x) (ensure-float (- *window-height* y)))
(let ((location (enemy-field-ray-intersect v1 v2)))
(if location
;; right click for ping any other click fires a missile
(if (eql button 3)
(make-ping location)
(fire-missile v1 v2 location))
;; if click wasn't on the enemy's field, check if it is on player's field
(let ((location (player-field-ray-intersect v1 v2)))
(when location (place-ship v1 v2 location (if (eql button 1) :vertical :horizontal))))))))

(:mousemotion
(:x x :y y :state state)

;; ping radius is being determined until mousebuttonup button 3
(when (eql state 4)
(multiple-value-bind (v1 v2)(get-3d-ray-under-mouse (ensure-float x) (ensure-float (- *window-height* y)))
(let ((location (enemy-field-ray-intersect v1 v2))
(pos (pos *ping*)))
(when location
;; new radius is distance of mouse from pos
(setf (radius *ping*) (sqrt (+ (expt (- (aref location 0) (aref pos 0)) 2) (expt (- (aref location 1) (aref pos 1)) 2)) )))))))

(:idle ()
(progn
(start-server server-ip port)
(setf *last-time* (sdl2:get-ticks))
(unwind-protect
(sdl2:with-event-loop (:method :poll)
(:idle
()
(setf *current-time* (sdl2:get-ticks))
(setf *delta-time* (ensure-float (- *current-time* *last-time*)))

(when (>= *delta-time* 10.0)
(incf *last-time* 10))

(network)
(when (connected-p)))

(:quit () t))

(stop-server)))
(progn
(connect-to-server server-ip port)
(sdl2:with-window (win :title (if (server-p) "Battleship Server" "Battleship Client") :w *window-width* :h *window-height* :flags '(:shown :opengl))
(sdl2:with-gl-context (gl-context win)
(sdl2:gl-make-current win gl-context)

(setf *graphics-engine* (make-instance 'graphics-engine))
(graphics-init *graphics-engine*)

(setf *last-time* (sdl2:get-ticks))
(unwind-protect

(sdl2:with-event-loop (:method :poll)

(:keydown
(:keysym keysym)
(let ((scancode (sdl2:scancode-value keysym))
(sym (sdl2:sym-value keysym))
(mod-value (sdl2:mod-value keysym)))))

(:mousebuttondown
(:x x :y y :button button)
;; two vectors needed to trace a ray from mouse click into the 3D world
(multiple-value-bind (v1 v2) (get-3d-ray-under-mouse (ensure-float x) (ensure-float (- *window-height* y)))
(let ((location (enemy-field-ray-intersect v1 v2)))
(if location
;; right click for ping any other click fires a missile
(if (eql button 3)
(make-ping location)
(fire-missile v1 v2 location))
;; if click wasn't on the enemy's field, check if it is on player's field
(let ((location (player-field-ray-intersect v1 v2)))
(when location (place-ship v1 v2 location (if (eql button 1) :vertical :horizontal))))))))

(:mousemotion
(:x x :y y :state state)
;; ping radius is being determined until right click release
(when (eql state 4)
(multiple-value-bind (v1 v2)(get-3d-ray-under-mouse (ensure-float x) (ensure-float (- *window-height* y)))
(let ((location (enemy-field-ray-intersect v1 v2))
(pos (pos *ping*)))
(when location
;; new radius is distance of mouse from pos
(setf (radius *ping*) (sqrt (+ (expt (- (aref location 0) (aref pos 0)) 2) (expt (- (aref location 1) (aref pos 1)) 2)) )))))))

(:idle
()
(setf *current-time* (sdl2:get-ticks))
(setf *delta-time* (ensure-float (- *current-time* *last-time*)))

(when (>= *delta-time* 10.0)

(incf *last-time* 10))

(network)

(when (connected-p)

(render-scene *graphics-engine*)
(sdl2:gl-swap-window win)))

(:quit () t))
(if (server-p)
(stop-server)
(disconnect-from-server)))))))
(:quit () t))

(disconnect-from-server))))))))

(defun enemy-field-ray-intersect (v1 v2)
(let ((distance (or (ray-triangle-collision v1
Expand Down
1 change: 0 additions & 1 deletion ping.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
(with-slots (pos radius) self
(ray-sphere-collision pos (* 2 radius) v1 v2)))


(defun make-ping (location)
(let ((location (sb-cga:vec+ location (sb-cga:vec 0.0 0.0 -1.0))))
(setf *ping* (make-instance 'ping :pos location))))
Expand Down

0 comments on commit dea3cd1

Please sign in to comment.